X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/00ecef01d7dd1f53c58ddeb0f906f3ec602283f0..refs/heads/master:/print-hncp.c diff --git a/print-hncp.c b/print-hncp.c index 7a3a6f72..2dc3147a 100644 --- a/print-hncp.c +++ b/print-hncp.c @@ -28,13 +28,10 @@ /* \summary: Home Networking Control Protocol (HNCP) printer */ -#ifdef HAVE_CONFIG_H #include -#endif #include "netdissect-stdinc.h" -#include #include #include "netdissect.h" @@ -157,28 +154,28 @@ is_ipv4_mapped_address(const u_char *addr) } static const char * -format_nid(const u_char *data) +format_nid(netdissect_options *ndo, const u_char *data) { static char buf[4][sizeof("01:01:01:01")]; static int i = 0; i = (i + 1) % 4; - nd_snprintf(buf[i], sizeof(buf[i]), "%02x:%02x:%02x:%02x", - EXTRACT_U_1(data), EXTRACT_U_1(data + 1), EXTRACT_U_1(data + 2), - EXTRACT_U_1(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][sizeof("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")]; static int i = 0; i = (i + 1) % 4; - nd_snprintf(buf[i], sizeof(buf[i]), "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64, - EXTRACT_BE_U_8(data), - EXTRACT_BE_U_8(data + 8), - EXTRACT_BE_U_8(data + 16), - EXTRACT_BE_U_8(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]; } @@ -189,7 +186,7 @@ format_interval(const uint32_t n) static char buf[4][sizeof("0000000.000s")]; static int i = 0; i = (i + 1) % 4; - nd_snprintf(buf[i], sizeof(buf[i]), "%u.%03us", n / 1000, n % 1000); + snprintf(buf[i], sizeof(buf[i]), "%u.%03us", n / 1000, n % 1000); return buf[i]; } @@ -197,9 +194,9 @@ static const char * format_ip6addr(netdissect_options *ndo, const u_char *cp) { if (is_ipv4_mapped_address(cp)) - return ipaddr_string(ndo, cp + IPV4_MAPPED_HEADING_LEN); + return GET_IPADDR_STRING(cp + IPV4_MAPPED_HEADING_LEN); else - return ip6addr_string(ndo, cp); + return GET_IP6ADDR_STRING(cp); } static int @@ -208,12 +205,12 @@ 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")]; - if (EXTRACT_U_1(prefix) >= 96 && max_length >= IPV4_MAPPED_HEADING_LEN + 1 && + if (GET_U_1(prefix) >= 96 && max_length >= IPV4_MAPPED_HEADING_LEN + 1 && is_ipv4_mapped_address(prefix + 1)) { - struct in_addr addr; + nd_ipv4 addr; u_int plen; - plen = EXTRACT_U_1(prefix) - 96; + plen = GET_U_1(prefix) - 96; if (32 < plen) return -1; max_length -= 1; @@ -227,10 +224,12 @@ print_prefix(netdissect_options *ndo, const u_char *prefix, u_int max_length) ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } - nd_snprintf(buf, sizeof(buf), "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen); + 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("%s", buf); @@ -243,7 +242,7 @@ print_dns_label(netdissect_options *ndo, { u_int length = 0; while (length < max_length) { - u_int lab_length = EXTRACT_U_1(cp + length); + u_int lab_length = GET_U_1(cp + length); length++; if (lab_length == 0) return (int)length; @@ -251,11 +250,11 @@ print_dns_label(netdissect_options *ndo, ND_PRINT("."); if (length+lab_length > max_length) { if (print) - (void)nd_printzp(ndo, cp+length, max_length-length, NULL); + nd_printjnp(ndo, cp+length, max_length-length); break; } if (print) - (void)nd_printzp(ndo, cp+length, lab_length, NULL); + nd_printjnp(ndo, cp+length, lab_length); length += lab_length; } if (print) @@ -276,8 +275,8 @@ dhcpv4_print(netdissect_options *ndo, if (i + 2 > length) return -1; tlv = cp + i; - type = EXTRACT_U_1(tlv); - optlen = EXTRACT_U_1(tlv + 1); + type = GET_U_1(tlv); + optlen = GET_U_1(tlv + 1); value = tlv + 2; ND_PRINT("\n"); @@ -296,14 +295,14 @@ dhcpv4_print(netdissect_options *ndo, return -1; } for (t = 0; t < optlen; t += 4) - ND_PRINT(" %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(" "); - if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL) + if ((tp = fqdn_print(ndo, tp, value + optlen)) == NULL) return -1; } } @@ -328,8 +327,8 @@ dhcpv6_print(netdissect_options *ndo, if (i + 4 > length) return -1; tlv = cp + i; - type = EXTRACT_BE_U_2(tlv); - optlen = EXTRACT_BE_U_2(tlv + 2); + type = GET_BE_U_2(tlv); + optlen = GET_BE_U_2(tlv + 2); value = tlv + 4; ND_PRINT("\n"); @@ -345,18 +344,18 @@ dhcpv6_print(netdissect_options *ndo, case DH6OPT_DNS_SERVERS: case DH6OPT_SNTP_SERVERS: { if (optlen % 16 != 0) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); return -1; } for (t = 0; t < optlen; t += 16) - ND_PRINT(" %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(" "); - if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL) + if ((tp = fqdn_print(ndo, tp, value + optlen)) == NULL) return -1; } } @@ -429,8 +428,8 @@ hncp_print_rec(netdissect_options *ndo, if (i + 4 > length) goto invalid; - type = EXTRACT_BE_U_2(tlv); - bodylen = EXTRACT_BE_U_2(tlv + 2); + type = GET_BE_U_2(tlv); + bodylen = GET_BE_U_2(tlv + 2); value = tlv + 4; ND_TCHECK_LEN(value, bodylen); if (i + bodylen + 4 > length) @@ -479,17 +478,17 @@ hncp_print_rec(netdissect_options *ndo, case DNCP_REQUEST_NETWORK_STATE: { if (bodylen != 0) - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); } break; case DNCP_REQUEST_NODE_STATE: { const char *node_identifier; if (bodylen != 4) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - node_identifier = format_nid(value); + node_identifier = format_nid(ndo, value); ND_PRINT(" NID: %s", node_identifier); } break; @@ -498,11 +497,11 @@ hncp_print_rec(netdissect_options *ndo, const char *node_identifier; uint32_t endpoint_identifier; if (bodylen != 8) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - node_identifier = format_nid(value); - endpoint_identifier = EXTRACT_BE_U_4(value + 4); + node_identifier = format_nid(ndo, value); + endpoint_identifier = GET_BE_U_4(value + 4); ND_PRINT(" NID: %s EPID: %08x", node_identifier, endpoint_identifier @@ -513,10 +512,10 @@ hncp_print_rec(netdissect_options *ndo, case DNCP_NETWORK_STATE: { uint64_t hash; if (bodylen != 8) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - hash = EXTRACT_BE_U_8(value); + hash = GET_BE_U_8(value); ND_PRINT(" hash: %016" PRIx64, hash); } break; @@ -526,13 +525,13 @@ hncp_print_rec(netdissect_options *ndo, uint32_t sequence_number; uint64_t hash; if (bodylen < 20) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - node_identifier = format_nid(value); - sequence_number = EXTRACT_BE_U_4(value + 4); - interval = format_interval(EXTRACT_BE_U_4(value + 8)); - hash = EXTRACT_BE_U_8(value + 12); + 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, @@ -547,12 +546,12 @@ hncp_print_rec(netdissect_options *ndo, const char *peer_node_identifier; uint32_t peer_endpoint_identifier, endpoint_identifier; if (bodylen != 12) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - peer_node_identifier = format_nid(value); - peer_endpoint_identifier = EXTRACT_BE_U_4(value + 4); - endpoint_identifier = EXTRACT_BE_U_4(value + 8); + 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, @@ -565,11 +564,11 @@ hncp_print_rec(netdissect_options *ndo, uint32_t endpoint_identifier; const char *interval; if (bodylen < 8) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - endpoint_identifier = EXTRACT_BE_U_4(value); - interval = format_interval(EXTRACT_BE_U_4(value + 4)); + 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 @@ -579,13 +578,13 @@ hncp_print_rec(netdissect_options *ndo, case DNCP_TRUST_VERDICT: { if (bodylen <= 36) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } ND_PRINT(" Verdict: %u Fingerprint: %s Common Name: ", - EXTRACT_U_1(value), - format_256(value + 4)); - (void)nd_printzp(ndo, value + 36, bodylen - 36, NULL); + GET_U_1(value), + format_256(ndo, value + 4)); + nd_printjnp(ndo, value + 36, bodylen - 36); } break; @@ -593,10 +592,10 @@ hncp_print_rec(netdissect_options *ndo, uint16_t capabilities; uint8_t M, P, H, L; if (bodylen < 5) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - capabilities = EXTRACT_BE_U_2(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); @@ -604,7 +603,7 @@ hncp_print_rec(netdissect_options *ndo, ND_PRINT(" M: %u P: %u H: %u L: %u User-agent: ", M, P, H, L ); - (void)nd_printzp(ndo, value + 4, bodylen - 4, NULL); + nd_printjnp(ndo, value + 4, bodylen - 4); } break; @@ -616,13 +615,13 @@ hncp_print_rec(netdissect_options *ndo, case HNCP_DELEGATED_PREFIX: { int l; - if (bodylen < 9 || bodylen < 9 + (EXTRACT_U_1(value + 8) + 7) / 8) { - ND_PRINT(" %s", istr); + if (bodylen < 9 || bodylen < 9 + (GET_U_1(value + 8) + 7) / 8) { + nd_print_invalid(ndo); break; } ND_PRINT(" VLSO: %s PLSO: %s Prefix: ", - format_interval(EXTRACT_BE_U_4(value)), - format_interval(EXTRACT_BE_U_4(value + 4)) + 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) { @@ -639,7 +638,7 @@ hncp_print_rec(netdissect_options *ndo, * IPv6", or -3, meaning "the prefix runs past * the end of the TLV". */ - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } l += 8 + (-l & 3); @@ -653,14 +652,14 @@ hncp_print_rec(netdissect_options *ndo, uint8_t policy; int l; if (bodylen < 1) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - policy = EXTRACT_U_1(value); + policy = GET_U_1(value); ND_PRINT(" type: "); if (policy == 0) { if (bodylen != 1) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } ND_PRINT("Internet connectivity"); @@ -681,7 +680,7 @@ hncp_print_rec(netdissect_options *ndo, * IPv6", or -3, meaning "the prefix runs past * the end of the TLV". */ - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } } else if (policy == 129) { @@ -689,10 +688,10 @@ hncp_print_rec(netdissect_options *ndo, print_dns_label(ndo, value+1, bodylen-1, 1); } else if (policy == 130) { ND_PRINT("Opaque UTF-8: "); - (void)nd_printzp(ndo, value + 1, bodylen - 1, NULL); + nd_printjnp(ndo, value + 1, bodylen - 1); } else if (policy == 131) { if (bodylen != 1) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } ND_PRINT("Restrictive assignment"); @@ -704,7 +703,7 @@ hncp_print_rec(netdissect_options *ndo, case HNCP_DHCPV4_DATA: { if (bodylen == 0) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } if (dhcpv4_print(ndo, value, bodylen, indent+1) != 0) @@ -714,11 +713,11 @@ hncp_print_rec(netdissect_options *ndo, case HNCP_DHCPV6_DATA: { if (bodylen == 0) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } if (dhcpv6_print(ndo, value, bodylen, indent+1) != 0) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } } @@ -727,18 +726,18 @@ hncp_print_rec(netdissect_options *ndo, case HNCP_ASSIGNED_PREFIX: { uint8_t prty; int l; - if (bodylen < 6 || bodylen < 6 + (EXTRACT_U_1(value + 5) + 7) / 8) { - ND_PRINT(" %s", istr); + if (bodylen < 6 || bodylen < 6 + (GET_U_1(value + 5) + 7) / 8) { + nd_print_invalid(ndo); break; } - prty = EXTRACT_U_1(value + 4) & 0xf; + prty = GET_U_1(value + 4) & 0xf; ND_PRINT(" EPID: %08x Prty: %u", - EXTRACT_BE_U_4(value), + GET_BE_U_4(value), prty ); ND_PRINT(" Prefix: "); if ((l = print_prefix(ndo, value + 5, bodylen - 5)) < 0) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } l += 5; @@ -753,10 +752,10 @@ hncp_print_rec(netdissect_options *ndo, uint32_t endpoint_identifier; const char *ip_address; if (bodylen < 20) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - endpoint_identifier = EXTRACT_BE_U_4(value); + endpoint_identifier = GET_BE_U_4(value); ip_address = format_ip6addr(ndo, value + 4); ND_PRINT(" EPID: %08x IP Address: %s", endpoint_identifier, @@ -771,19 +770,19 @@ hncp_print_rec(netdissect_options *ndo, const char *ip_address; int len; if (bodylen < 17) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } ip_address = format_ip6addr(ndo, value); ND_PRINT(" IP-Address: %s %c%c%c ", ip_address, - (EXTRACT_U_1(value + 16) & 4) ? 'l' : '-', - (EXTRACT_U_1(value + 16) & 2) ? 'b' : '-', - (EXTRACT_U_1(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(" %s", istr); + nd_print_invalid(ndo); break; } len += 17; @@ -795,7 +794,7 @@ hncp_print_rec(netdissect_options *ndo, case HNCP_DOMAIN_NAME: { if (bodylen == 0) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } ND_PRINT(" Domain: "); @@ -806,12 +805,12 @@ hncp_print_rec(netdissect_options *ndo, case HNCP_NODE_NAME: { u_int l; if (bodylen < 17) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - l = EXTRACT_U_1(value + 16); + l = GET_U_1(value + 16); if (bodylen < 17 + l) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } ND_PRINT(" IP-Address: %s Name: ", @@ -819,13 +818,13 @@ hncp_print_rec(netdissect_options *ndo, ); if (l < 64) { ND_PRINT("\""); - (void)nd_printzp(ndo, value + 17, l, NULL); + nd_printjnp(ndo, value + 17, l); ND_PRINT("\""); } else { - ND_PRINT("%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); } @@ -833,10 +832,10 @@ hncp_print_rec(netdissect_options *ndo, case HNCP_MANAGED_PSK: { if (bodylen < 32) { - ND_PRINT(" %s", istr); + nd_print_invalid(ndo); break; } - ND_PRINT(" PSK: %s", format_256(value)); + ND_PRINT(" PSK: %s", format_256(ndo, value)); hncp_print_rec(ndo, value + 32, bodylen - 32, indent+1); } break; @@ -850,7 +849,7 @@ 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); @@ -861,6 +860,5 @@ hncp_print_rec(netdissect_options *ndo, return; invalid: - ND_PRINT("%s", istr); - return; + nd_print_invalid(ndo); }