blob: 1e77a58f9d2f083d48f82e327b325eb0acc1d5db [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
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 *
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00008 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00009 */
10
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000011#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000012#include "dhcpd.h"
13
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000014
Mike Frysinger7031f622006-05-08 03:20:50 +000015/* 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 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000018void FAST_FUNC addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t ip)
Mike Frysinger7031f622006-05-08 03:20:50 +000019{
Mike Frysinger7031f622006-05-08 03:20:50 +000020 struct static_lease *new_static_lease;
21
22 /* Build new node */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000023 new_static_lease = xzalloc(sizeof(struct static_lease));
24 memcpy(new_static_lease->mac, mac, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +000025 new_static_lease->ip = ip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000026 /*new_static_lease->next = NULL;*/
Mike Frysinger7031f622006-05-08 03:20:50 +000027
28 /* If it's the first node to be added... */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000029 if (*lease_struct == NULL) {
Mike Frysinger7031f622006-05-08 03:20:50 +000030 *lease_struct = new_static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000031 } else {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000032 struct static_lease *cur = *lease_struct;
33 while (cur->next)
Mike Frysinger7031f622006-05-08 03:20:50 +000034 cur = cur->next;
Mike Frysinger7031f622006-05-08 03:20:50 +000035 cur->next = new_static_lease;
36 }
Mike Frysinger7031f622006-05-08 03:20:50 +000037}
38
39/* Check to see if a mac has an associated static lease */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000040uint32_t FAST_FUNC getIpByMac(struct static_lease *lease_struct, void *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000041{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000042 while (lease_struct) {
43 if (memcmp(lease_struct->mac, mac, 6) == 0)
44 return lease_struct->ip;
45 lease_struct = lease_struct->next;
Mike Frysinger7031f622006-05-08 03:20:50 +000046 }
47
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000048 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000049}
50
51/* Check to see if an ip is reserved as a static ip */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000052int FAST_FUNC reservedIp(struct static_lease *lease_struct, uint32_t ip)
Mike Frysinger7031f622006-05-08 03:20:50 +000053{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000054 while (lease_struct) {
55 if (lease_struct->ip == ip)
56 return 1;
57 lease_struct = lease_struct->next;
Mike Frysinger7031f622006-05-08 03:20:50 +000058 }
59
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000060 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000061}
62
Denis Vlasenko35a064b2008-11-06 00:49:59 +000063#if ENABLE_UDHCP_DEBUG
Mike Frysinger7031f622006-05-08 03:20:50 +000064/* Print out static leases just to check what's going on */
65/* Takes the address of the pointer to the static_leases linked list */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000066void FAST_FUNC printStaticLeases(struct static_lease **arg)
Mike Frysinger7031f622006-05-08 03:20:50 +000067{
Mike Frysinger7031f622006-05-08 03:20:50 +000068 struct static_lease *cur = *arg;
69
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000070 while (cur) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000071 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);
Mike Frysinger7031f622006-05-08 03:20:50 +000076 cur = cur->next;
77 }
Mike Frysinger7031f622006-05-08 03:20:50 +000078}
79#endif