Added hex printing of the packets if command-line options are -xxx
authoraland <aland>
Tue, 1 Nov 2005 23:15:25 +0000 (23:15 +0000)
committeraland <aland>
Tue, 1 Nov 2005 23:15:25 +0000 (23:15 +0000)
src/main/radclient.c

index e3c82d0..9993530 100644 (file)
@@ -470,6 +470,65 @@ static void request_free(void *data)
 }
 
 
+static void print_hex(RADIUS_PACKET *packet)
+{
+       int i;
+
+       printf("  Code:\t\t%u\n", packet->data[0]);
+       printf("  Id:\t\t%u\n", packet->data[1]);
+       printf("  Length:\t%u\n", ((packet->data[2] << 8) |
+                                  (packet->data[3])));
+       printf("  Vector:\t");
+       for (i = 4; i < 20; i++) {
+               printf("%02x", packet->data[i]);
+       }
+       printf("\n");
+       
+       if (packet->data_len > 20) {
+               int total;
+               const uint8_t *ptr;
+               printf("  Data:");
+
+               total = packet->data_len - 20;
+               ptr = packet->data + 20;
+
+               while (total > 0) {
+                       int attrlen;
+
+                       printf("\t\t");
+                       if (total < 2) { /* too short */
+                               printf("%02x\n", *ptr);
+                               break;
+                       }
+
+                       if (ptr[1] > total) { /* too long */
+                               for (i = 0; i < total; i++) {
+                                       printf("%02x ", ptr[i]);
+                               }
+                               break;
+                       }
+
+                       printf("%02x  %02x  ", ptr[0], ptr[1]);
+                       attrlen = ptr[1] - 2;
+                       ptr += 2;
+                       total -= 2;
+
+                       for (i = 0; i < attrlen; i++) {
+                               if ((i > 0) && ((i & 0x0f) == 0x00))
+                                       printf("\t\t\t");
+                               printf("%02x ", ptr[i]);
+                               if ((i & 0x0f) == 0x0f) printf("\n");
+                       }
+
+                       if ((attrlen & 0x0f) != 0x00) printf("\n");
+
+                       ptr += attrlen;
+                       total -= attrlen;
+               }
+       }
+       fflush(stdout);
+}
+
 /*
  *     Send one packet.
  */
@@ -631,6 +690,8 @@ static int send_one_packet(radclient_t *radclient)
                        radclient->request->id, librad_errstr);
        }
 
+       if (librad_debug > 2) print_hex(radclient->request);
+
        return 0;
 }
 
@@ -674,6 +735,8 @@ static int recv_one_packet(int wait_time)
                return -1;      /* bad packet */
        }
 
+       if (librad_debug > 2) print_hex(reply);
+
        myclient.request = &myrequest;
        myrequest.id = reply->id;
        myrequest.dst_ipaddr = reply->src_ipaddr;
@@ -681,7 +744,7 @@ static int recv_one_packet(int wait_time)
 
        node = rbtree_find(request_tree, &myclient);
        if (!node) {
-               fprintf(stderr, "radclient: received response to request we did not send.\n");
+               fprintf(stderr, "radclient: received response to request we did not send. (%d)\n", myrequest.id);
                rad_free(&reply);
                return -1;      /* got reply to packet we didn't send */
        }