dumpleases.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <fcntl.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <sys/wait.h>
  6. #include <arpa/inet.h>
  7. #include <netdb.h>
  8. #include <netinet/in.h>
  9. #include <stdio.h>
  10. #include <sys/socket.h>
  11. #include <unistd.h>
  12. #include <getopt.h>
  13. #include <time.h>
  14. #include "dhcpd.h"
  15. #include "leases.h"
  16. #include "libbb_udhcp.h"
  17. #define REMAINING 0
  18. #define ABSOLUTE 1
  19. #ifndef IN_BUSYBOX
  20. static void __attribute__ ((noreturn)) show_usage(void)
  21. {
  22. printf(
  23. "Usage: dumpleases -f <file> -[r|a]\n\n"
  24. " -f, --file=FILENAME Leases file to load\n"
  25. " -r, --remaining Interepret lease times as time remaing\n"
  26. " -a, --absolute Interepret lease times as expire time\n");
  27. exit(0);
  28. }
  29. #else
  30. #define show_usage bb_show_usage
  31. #endif
  32. #ifdef IN_BUSYBOX
  33. int dumpleases_main(int argc, char *argv[])
  34. #else
  35. int main(int argc, char *argv[])
  36. #endif
  37. {
  38. FILE *fp;
  39. int i, c, mode = REMAINING;
  40. long expires;
  41. const char *file = LEASES_FILE;
  42. struct dhcpOfferedAddr lease;
  43. struct in_addr addr;
  44. static const struct option options[] = {
  45. {"absolute", 0, 0, 'a'},
  46. {"remaining", 0, 0, 'r'},
  47. {"file", 1, 0, 'f'},
  48. {0, 0, 0, 0}
  49. };
  50. while (1) {
  51. int option_index = 0;
  52. c = getopt_long(argc, argv, "arf:", options, &option_index);
  53. if (c == -1) break;
  54. switch (c) {
  55. case 'a': mode = ABSOLUTE; break;
  56. case 'r': mode = REMAINING; break;
  57. case 'f':
  58. file = optarg;
  59. break;
  60. default:
  61. show_usage();
  62. }
  63. }
  64. fp = xfopen(file, "r");
  65. printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
  66. /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
  67. while (fread(&lease, sizeof(lease), 1, fp)) {
  68. for (i = 0; i < 6; i++) {
  69. printf("%02x", lease.chaddr[i]);
  70. if (i != 5) printf(":");
  71. }
  72. addr.s_addr = lease.yiaddr;
  73. printf(" %-15s", inet_ntoa(addr));
  74. expires = ntohl(lease.expires);
  75. printf(" ");
  76. if (mode == REMAINING) {
  77. if (!expires) printf("expired\n");
  78. else {
  79. if (expires > 60*60*24) {
  80. printf("%ld days, ", expires / (60*60*24));
  81. expires %= 60*60*24;
  82. }
  83. if (expires > 60*60) {
  84. printf("%ld hours, ", expires / (60*60));
  85. expires %= 60*60;
  86. }
  87. if (expires > 60) {
  88. printf("%ld minutes, ", expires / 60);
  89. expires %= 60;
  90. }
  91. printf("%ld seconds\n", expires);
  92. }
  93. } else printf("%s", ctime(&expires));
  94. }
  95. fclose(fp);
  96. return 0;
  97. }