dumpleases.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <getopt.h>
  6. #include "common.h"
  7. #include "dhcpd.h"
  8. int dumpleases_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  9. int dumpleases_main(int argc ATTRIBUTE_UNUSED, char **argv)
  10. {
  11. int fd;
  12. int i;
  13. unsigned opt;
  14. time_t expires;
  15. const char *file = LEASES_FILE;
  16. struct dhcpOfferedAddr 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_GETOPT_LONG
  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. opt_complementary = "=0:a--r:r--a";
  32. opt = getopt32(argv, "arf:", &file);
  33. fd = xopen(file, O_RDONLY);
  34. printf("Mac Address IP-Address Expires %s\n", (opt & OPT_a) ? "at" : "in");
  35. /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
  36. while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
  37. printf(":%02x"+1, lease.chaddr[0]);
  38. for (i = 1; i < 6; i++) {
  39. printf(":%02x", lease.chaddr[i]);
  40. }
  41. addr.s_addr = lease.yiaddr;
  42. printf(" %-15s ", inet_ntoa(addr));
  43. expires = ntohl(lease.expires);
  44. if (!(opt & OPT_a)) { /* no -a */
  45. if (!expires)
  46. puts("expired");
  47. else {
  48. unsigned d, h, m;
  49. d = expires / (24*60*60); expires %= (24*60*60);
  50. h = expires / (60*60); expires %= (60*60);
  51. m = expires / 60; expires %= 60;
  52. if (d) printf("%u days ", d);
  53. printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
  54. }
  55. } else /* -a */
  56. fputs(ctime(&expires), stdout);
  57. }
  58. /* close(fd); */
  59. return 0;
  60. }