busybox/networking/udhcp/static_leases.c
<<
>>
Prefs
   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 * Licensed under GPLv2, see file LICENSE in this tarball for details.
   9 */
  10
  11#include "common.h"
  12#include "dhcpd.h"
  13
  14
  15/* Takes the address of the pointer to the static_leases linked list,
  16 *   Address to a 6 byte mac address
  17 *   Address to a 4 byte ip address */
  18void FAST_FUNC addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t ip)
  19{
  20        struct static_lease *new_static_lease;
  21
  22        /* Build new node */
  23        new_static_lease = xzalloc(sizeof(struct static_lease));
  24        memcpy(new_static_lease->mac, mac, 6);
  25        new_static_lease->ip = ip;
  26        /*new_static_lease->next = NULL;*/
  27
  28        /* If it's the first node to be added... */
  29        if (*lease_struct == NULL) {
  30                *lease_struct = new_static_lease;
  31        } else {
  32                struct static_lease *cur = *lease_struct;
  33                while (cur->next)
  34                        cur = cur->next;
  35                cur->next = new_static_lease;
  36        }
  37}
  38
  39/* Check to see if a mac has an associated static lease */
  40uint32_t FAST_FUNC getIpByMac(struct static_lease *lease_struct, void *mac)
  41{
  42        while (lease_struct) {
  43                if (memcmp(lease_struct->mac, mac, 6) == 0)
  44                        return lease_struct->ip;
  45                lease_struct = lease_struct->next;
  46        }
  47
  48        return 0;
  49}
  50
  51/* Check to see if an ip is reserved as a static ip */
  52int FAST_FUNC reservedIp(struct static_lease *lease_struct, uint32_t ip)
  53{
  54        while (lease_struct) {
  55                if (lease_struct->ip == ip)
  56                        return 1;
  57                lease_struct = lease_struct->next;
  58        }
  59
  60        return 0;
  61}
  62
  63#if ENABLE_UDHCP_DEBUG
  64/* Print out static leases just to check what's going on */
  65/* Takes the address of the pointer to the static_leases linked list */
  66void FAST_FUNC printStaticLeases(struct static_lease **arg)
  67{
  68        struct static_lease *cur = *arg;
  69
  70        while (cur) {
  71                printf("PrintStaticLeases: Lease mac Value: %02x:%02x:%02x:%02x:%02x:%02x\n",
  72                        cur->mac[0], cur->mac[1], cur->mac[2],
  73                        cur->mac[3], cur->mac[4], cur->mac[5]
  74                );
  75                printf("PrintStaticLeases: Lease ip Value: %x\n", cur->ip);
  76                cur = cur->next;
  77        }
  78}
  79#endif
  80