]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add and use fr_dhcpv4_print_hex
authorAlan T. DeKok <aland@freeradius.org>
Wed, 21 Nov 2018 12:13:32 +0000 (07:13 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 21 Nov 2018 12:13:32 +0000 (07:13 -0500)
src/modules/proto_dhcpv4/proto_dhcpv4.c
src/protocols/dhcpv4/base.c
src/protocols/dhcpv4/dhcpv4.h

index 659f2e84abc008aa06164b290b3718d087161b48..bdb20f65f5f929c55cec443b0e2d5027237c3584 100644 (file)
@@ -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;
 }
index 8fb7b4bf25f4400099cfed54dec45ef1774965a2..beaa440ad6f11119feb41d9354b955b819cd9e45 100644 (file)
@@ -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;
+       }
+}
index 6b529018e9c9d96121e9e746c760f1e3878a1259..e1cab8ffdbc8fc77edbdc1faaad319ecfb8b6221 100644 (file)
@@ -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