static_leases.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Storing and retrieving data for static leases
  4. *
  5. * Wade Berrier <wberrier@myrealbox.com> September 2004
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  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. * 4 byte IP address */
  14. void FAST_FUNC add_static_lease(struct static_lease **st_lease_pp,
  15. uint8_t *mac,
  16. uint32_t nip)
  17. {
  18. struct static_lease *st_lease;
  19. /* Find the tail of the list */
  20. while ((st_lease = *st_lease_pp) != NULL) {
  21. st_lease_pp = &st_lease->next;
  22. }
  23. /* Add new node */
  24. *st_lease_pp = st_lease = xzalloc(sizeof(*st_lease));
  25. memcpy(st_lease->mac, mac, 6);
  26. st_lease->nip = nip;
  27. /*st_lease->next = NULL;*/
  28. }
  29. /* Find static lease IP by mac */
  30. uint32_t FAST_FUNC get_static_nip_by_mac(struct static_lease *st_lease, void *mac)
  31. {
  32. while (st_lease) {
  33. if (memcmp(st_lease->mac, mac, 6) == 0)
  34. return st_lease->nip;
  35. st_lease = st_lease->next;
  36. }
  37. return 0;
  38. }
  39. /* Check to see if an IP is reserved as a static IP */
  40. int FAST_FUNC is_nip_reserved(struct static_lease *st_lease, uint32_t nip)
  41. {
  42. while (st_lease) {
  43. if (st_lease->nip == nip)
  44. return 1;
  45. st_lease = st_lease->next;
  46. }
  47. return 0;
  48. }
  49. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  50. /* Print out static leases just to check what's going on */
  51. /* Takes the address of the pointer to the static_leases linked list */
  52. void FAST_FUNC log_static_leases(struct static_lease **st_lease_pp)
  53. {
  54. struct static_lease *cur;
  55. if (dhcp_verbose < 2)
  56. return;
  57. cur = *st_lease_pp;
  58. while (cur) {
  59. bb_error_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x",
  60. cur->mac[0], cur->mac[1], cur->mac[2],
  61. cur->mac[3], cur->mac[4], cur->mac[5],
  62. cur->nip
  63. );
  64. cur = cur->next;
  65. }
  66. }
  67. #endif