dumpleases.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  8. int dumpleases_main(int argc UNUSED_PARAM, char **argv)
  9. {
  10. int fd;
  11. int i;
  12. unsigned opt;
  13. time_t expires;
  14. const char *file = LEASES_FILE;
  15. struct dhcpOfferedAddr lease;
  16. struct in_addr addr;
  17. enum {
  18. OPT_a = 0x1, // -a
  19. OPT_r = 0x2, // -r
  20. OPT_f = 0x4, // -f
  21. };
  22. #if ENABLE_GETOPT_LONG
  23. static const char dumpleases_longopts[] ALIGN1 =
  24. "absolute\0" No_argument "a"
  25. "remaining\0" No_argument "r"
  26. "file\0" Required_argument "f"
  27. ;
  28. applet_long_options = dumpleases_longopts;
  29. #endif
  30. opt_complementary = "=0:a--r:r--a";
  31. opt = getopt32(argv, "arf:", &file);
  32. fd = xopen(file, O_RDONLY);
  33. printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in");
  34. /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
  35. while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
  36. printf(":%02x"+1, lease.chaddr[0]);
  37. for (i = 1; i < 6; i++) {
  38. printf(":%02x", lease.chaddr[i]);
  39. }
  40. addr.s_addr = lease.yiaddr;
  41. printf(" %-15s ", inet_ntoa(addr));
  42. expires = ntohl(lease.expires);
  43. if (!(opt & OPT_a)) { /* no -a */
  44. if (!expires)
  45. puts("expired");
  46. else {
  47. unsigned d, h, m;
  48. d = expires / (24*60*60); expires %= (24*60*60);
  49. h = expires / (60*60); expires %= (60*60);
  50. m = expires / 60; expires %= 60;
  51. if (d) printf("%u days ", d);
  52. printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
  53. }
  54. } else /* -a */
  55. fputs(ctime(&expires), stdout);
  56. }
  57. /* close(fd); */
  58. return 0;
  59. }