dumpleases.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #define REMAINING 0
  9. #define ABSOLUTE 1
  10. int dumpleases_main(int argc, char *argv[])
  11. {
  12. int fp;
  13. int i, c, mode = REMAINING;
  14. unsigned long expires;
  15. const char *file = LEASES_FILE;
  16. struct dhcpOfferedAddr lease;
  17. struct in_addr addr;
  18. static const struct option options[] = {
  19. {"absolute", 0, 0, 'a'},
  20. {"remaining", 0, 0, 'r'},
  21. {"file", 1, 0, 'f'},
  22. {0, 0, 0, 0}
  23. };
  24. while (1) {
  25. int option_index = 0;
  26. c = getopt_long(argc, argv, "arf:", options, &option_index);
  27. if (c == -1) break;
  28. switch (c) {
  29. case 'a': mode = ABSOLUTE; break;
  30. case 'r': mode = REMAINING; break;
  31. case 'f':
  32. file = optarg;
  33. break;
  34. default:
  35. bb_show_usage();
  36. }
  37. }
  38. fp = xopen(file, O_RDONLY);
  39. printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
  40. /* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
  41. while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) {
  42. printf(":%02x"+1, lease.chaddr[0]);
  43. for (i = 1; i < 6; i++) {
  44. printf(":%02x", lease.chaddr[i]);
  45. }
  46. addr.s_addr = lease.yiaddr;
  47. printf(" %-15s ", inet_ntoa(addr));
  48. expires = ntohl(lease.expires);
  49. if (mode == REMAINING) {
  50. if (!expires)
  51. printf("expired\n");
  52. else {
  53. unsigned d, h, m;
  54. d = expires / (24*60*60); expires %= (24*60*60);
  55. h = expires / (60*60); expires %= (60*60);
  56. m = expires / 60; expires %= 60;
  57. if (d) printf("%u days ", d);
  58. printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
  59. }
  60. } else fputs(ctime(&expires), stdout);
  61. }
  62. /* close(fp); */
  63. return 0;
  64. }