dumpleases.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. #include "common.h"
  6. #include "dhcpd.h"
  7. #include "unicode.h"
  8. int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  9. int dumpleases_main(int argc UNUSED_PARAM, char **argv)
  10. {
  11. int fd;
  12. int i;
  13. unsigned opt;
  14. int64_t written_at, curr, expires_abs;
  15. const char *file = LEASES_FILE;
  16. struct dyn_lease lease;
  17. struct in_addr addr;
  18. enum {
  19. OPT_a = 0x1, // -a
  20. OPT_r = 0x2, // -r
  21. OPT_f = 0x4, // -f
  22. };
  23. #if ENABLE_LONG_OPTS
  24. static const char dumpleases_longopts[] ALIGN1 =
  25. "absolute\0" No_argument "a"
  26. "remaining\0" No_argument "r"
  27. "file\0" Required_argument "f"
  28. ;
  29. applet_long_options = dumpleases_longopts;
  30. #endif
  31. init_unicode();
  32. opt_complementary = "=0:a--r:r--a";
  33. opt = getopt32(argv, "arf:", &file);
  34. fd = xopen(file, O_RDONLY);
  35. printf("Mac Address IP Address Host Name Expires %s\n", (opt & OPT_a) ? "at" : "in");
  36. /* "00:00:00:00:00:00 255.255.255.255 ABCDEFGHIJKLMNOPQRS Wed Jun 30 21:49:08 1993" */
  37. /* "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 */
  38. xread(fd, &written_at, sizeof(written_at));
  39. written_at = SWAP_BE64(written_at);
  40. curr = time(NULL);
  41. if (curr < written_at)
  42. written_at = curr; /* lease file from future! :) */
  43. while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
  44. const char *fmt = ":%02x" + 1;
  45. for (i = 0; i < 6; i++) {
  46. printf(fmt, lease.lease_mac[i]);
  47. fmt = ":%02x";
  48. }
  49. addr.s_addr = lease.lease_nip;
  50. #if ENABLE_UNICODE_SUPPORT
  51. {
  52. char *uni_name = unicode_conv_to_printable_fixedwidth(NULL, lease.hostname, 19);
  53. printf(" %-16s%s ", inet_ntoa(addr), uni_name);
  54. free(uni_name);
  55. }
  56. #else
  57. /* actually, 15+1 and 19+1, +1 is a space between columns */
  58. /* lease.hostname is char[20] and is always NUL terminated */
  59. printf(" %-16s%-20s", inet_ntoa(addr), lease.hostname);
  60. #endif
  61. expires_abs = ntohl(lease.expires) + written_at;
  62. if (expires_abs <= curr) {
  63. puts("expired");
  64. continue;
  65. }
  66. if (!(opt & OPT_a)) { /* no -a */
  67. unsigned d, h, m;
  68. unsigned expires = expires_abs - curr;
  69. d = expires / (24*60*60); expires %= (24*60*60);
  70. h = expires / (60*60); expires %= (60*60);
  71. m = expires / 60; expires %= 60;
  72. if (d)
  73. printf("%u days ", d);
  74. printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
  75. } else { /* -a */
  76. time_t t = expires_abs;
  77. fputs(ctime(&t), stdout);
  78. }
  79. }
  80. /* close(fd); */
  81. return 0;
  82. }