dumpleases.c 2.7 KB

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