static_leases.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * static_leases.c -- Couple of functions to assist with storing and
  4. * retrieving data for static leases
  5. *
  6. * Wade Berrier <wberrier@myrealbox.com> September 2004
  7. *
  8. */
  9. #include "common.h"
  10. #include "dhcpd.h"
  11. /* Takes the address of the pointer to the static_leases linked list,
  12. * Address to a 6 byte mac address
  13. * Address to a 4 byte ip address */
  14. int addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip)
  15. {
  16. struct static_lease *cur;
  17. struct static_lease *new_static_lease;
  18. /* Build new node */
  19. new_static_lease = xmalloc(sizeof(struct static_lease));
  20. new_static_lease->mac = mac;
  21. new_static_lease->ip = ip;
  22. new_static_lease->next = NULL;
  23. /* If it's the first node to be added... */
  24. if (*lease_struct == NULL) {
  25. *lease_struct = new_static_lease;
  26. } else {
  27. cur = *lease_struct;
  28. while (cur->next) {
  29. cur = cur->next;
  30. }
  31. cur->next = new_static_lease;
  32. }
  33. return 1;
  34. }
  35. /* Check to see if a mac has an associated static lease */
  36. uint32_t getIpByMac(struct static_lease *lease_struct, void *arg)
  37. {
  38. uint32_t return_ip;
  39. struct static_lease *cur = lease_struct;
  40. uint8_t *mac = arg;
  41. return_ip = 0;
  42. while (cur) {
  43. /* If the client has the correct mac */
  44. if (memcmp(cur->mac, mac, 6) == 0) {
  45. return_ip = *(cur->ip);
  46. }
  47. cur = cur->next;
  48. }
  49. return return_ip;
  50. }
  51. /* Check to see if an ip is reserved as a static ip */
  52. uint32_t reservedIp(struct static_lease *lease_struct, uint32_t ip)
  53. {
  54. struct static_lease *cur = lease_struct;
  55. uint32_t return_val = 0;
  56. while (cur) {
  57. /* If the client has the correct ip */
  58. if (*cur->ip == ip)
  59. return_val = 1;
  60. cur = cur->next;
  61. }
  62. return return_val;
  63. }
  64. #if ENABLE_FEATURE_UDHCP_DEBUG
  65. /* Print out static leases just to check what's going on */
  66. /* Takes the address of the pointer to the static_leases linked list */
  67. void printStaticLeases(struct static_lease **arg)
  68. {
  69. /* Get a pointer to the linked list */
  70. struct static_lease *cur = *arg;
  71. while (cur) {
  72. /* printf("PrintStaticLeases: Lease mac Address: %x\n", cur->mac); */
  73. printf("PrintStaticLeases: Lease mac Value: %x\n", *(cur->mac));
  74. /* printf("PrintStaticLeases: Lease ip Address: %x\n", cur->ip); */
  75. printf("PrintStaticLeases: Lease ip Value: %x\n", *(cur->ip));
  76. cur = cur->next;
  77. }
  78. }
  79. #endif