d2d8d9cda38a6176ec860a5587fa02324a325f0c
[radsecproxy.git] / lib / debug.c
1 /* See the file COPYING for licensing information.  */
2
3 #include <stdio.h>
4 #include <freeradius/libradius.h>
5 #include "libradsec.h"
6 #include "libradsec-impl.h"
7 #include "debug.h"
8
9 /* From freeradius-server/src/lib/radius.c */
10 static void print_hex(RADIUS_PACKET *packet)
11 {
12         int i;
13
14         if (!packet->data) return;
15
16         printf("  Code:\t\t%u\n", packet->data[0]);
17         printf("  Id:\t\t%u\n", packet->data[1]);
18         printf("  Length:\t%u\n", ((packet->data[2] << 8) |
19                                    (packet->data[3])));
20         printf("  Vector:\t");
21         for (i = 4; i < 20; i++) {
22                 printf("%02x", packet->data[i]);
23         }
24         printf("\n");
25
26         if (packet->data_len > 20) {
27                 int total;
28                 const uint8_t *ptr;
29                 printf("  Data:");
30
31                 total = packet->data_len - 20;
32                 ptr = packet->data + 20;
33
34                 while (total > 0) {
35                         int attrlen;
36
37                         printf("\t\t");
38                         if (total < 2) { /* too short */
39                                 printf("%02x\n", *ptr);
40                                 break;
41                         }
42
43                         if (ptr[1] > total) { /* too long */
44                                 for (i = 0; i < total; i++) {
45                                         printf("%02x ", ptr[i]);
46                                 }
47                                 break;
48                         }
49
50                         printf("%02x  %02x  ", ptr[0], ptr[1]);
51                         attrlen = ptr[1] - 2;
52                         ptr += 2;
53                         total -= 2;
54
55                         for (i = 0; i < attrlen; i++) {
56                                 if ((i > 0) && ((i & 0x0f) == 0x00))
57                                         printf("\t\t\t");
58                                 printf("%02x ", ptr[i]);
59                                 if ((i & 0x0f) == 0x0f) printf("\n");
60                         }
61
62                         if ((attrlen & 0x0f) != 0x00) printf("\n");
63
64                         ptr += attrlen;
65                         total -= attrlen;
66                 }
67         }
68         fflush(stdout);
69 }
70
71 void
72 rs_dump_packet (const struct rs_packet *pkt)
73 {
74   print_hex (pkt->rpkt);
75 }
76
77 void
78 rs_dump_attr (const struct rs_attr *attr)
79 {
80   vp_printlist (stderr, attr->vp);
81 }