From: Alan T. DeKok Date: Wed, 21 Nov 2018 12:13:32 +0000 (-0500) Subject: add and use fr_dhcpv4_print_hex X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=63339b69eee3db8dcf773ced5d269d9dfc83288b;p=thirdparty%2Ffreeradius-server.git add and use fr_dhcpv4_print_hex --- diff --git a/src/modules/proto_dhcpv4/proto_dhcpv4.c b/src/modules/proto_dhcpv4/proto_dhcpv4.c index 659f2e84abc..bdb20f65f5f 100644 --- a/src/modules/proto_dhcpv4/proto_dhcpv4.c +++ b/src/modules/proto_dhcpv4/proto_dhcpv4.c @@ -277,15 +277,10 @@ static int mod_decode(void const *instance, REQUEST *request, uint8_t *const dat */ request->dict = dict_dhcpv4; -#if 0 - /* - * @todo - print hex packets here! - */ if (DEBUG_ENABLED3) { RDEBUG("proto_dhcpv4 decode packet"); fr_dhcpv4_print_hex(fr_log_fp, data, data_len); } -#endif client = address->radclient; @@ -428,12 +423,10 @@ static ssize_t mod_encode(void const *instance, REQUEST *request, uint8_t *buffe return -1; } -#if 0 if (DEBUG_ENABLED3) { RDEBUG("proto_dhcpv4 encode packet"); fr_dhcpv4_print_hex(fr_log_fp, buffer, data_len); } -#endif return data_len; } diff --git a/src/protocols/dhcpv4/base.c b/src/protocols/dhcpv4/base.c index 8fb7b4bf25f..beaa440ad6f 100644 --- a/src/protocols/dhcpv4/base.c +++ b/src/protocols/dhcpv4/base.c @@ -532,3 +532,67 @@ void fr_dhcpv4_free(void) fr_dict_autofree(dhcpv4_dict); } + +static char const *short_header_names[] = { + "opcode", + "hwtype", + "hwaddrlen", + "hop_count", + "xid", + "seconds", + "flags", + "ciaddr", + "yiaddr", + "siaddr", + "giaddr", + "chaddr", + "server_hostname", + "boot_filename", +}; + +static void print_hex_data(uint8_t const *ptr, int attrlen, int depth) +{ + int i; + static char const tabs[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; + + for (i = 0; i < attrlen; i++) { + if ((i > 0) && ((i & 0x0f) == 0x00)) + fprintf(fr_log_fp, "%.*s", depth, tabs); + fprintf(fr_log_fp, "%02x ", ptr[i]); + if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n"); + } + if ((i & 0x0f) != 0) fprintf(fr_log_fp, "\n"); +} + +/** Print a raw RADIUS packet as hex. + * + */ +void fr_dhcpv4_print_hex(FILE *fp, uint8_t const *packet, size_t packet_len) +{ + int i; + uint8_t const *attr, *end; + + end = packet + packet_len; + attr = packet; + + for (i = 0; i < 14; i++) { + fprintf(fp, "\t%s: ", short_header_names[i]); + print_hex_data(attr, dhcp_header_sizes[i], 2); + attr += dhcp_header_sizes[i]; + } + + while (attr < end) { + fprintf(fp, "\t\t"); + + fprintf(fp, "%02x %02x ", attr[0], attr[1]); + + print_hex_data(attr + 2, attr[1], 3); + + /* + * "End of option" option. + */ + if (attr[0] == 255) break; + + attr += attr[1] + 2; + } +} diff --git a/src/protocols/dhcpv4/dhcpv4.h b/src/protocols/dhcpv4/dhcpv4.h index 6b529018e9c..e1cab8ffdbc 100644 --- a/src/protocols/dhcpv4/dhcpv4.h +++ b/src/protocols/dhcpv4/dhcpv4.h @@ -147,6 +147,7 @@ RADIUS_PACKET *fr_dhcpv4_packet_alloc(uint8_t const *data, ssize_t data_len); ssize_t fr_dhcpv4_encode(uint8_t *buffer, size_t buflen, int code, uint32_t xid, VALUE_PAIR *vps); int fr_dhcpv4_init(void); void fr_dhcpv4_free(void); +void fr_dhcpv4_print_hex(FILE *fp, uint8_t const *packet, size_t packet_len); /* * decode.c