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
10#include "common.h"
11#include "dhcpd.h"
12
13
14/* Takes the address of the pointer to the static_leases linked list,
15 *   Address to a 6 byte mac address
16 *   Address to a 4 byte ip address */
17int addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip)
18{
19	struct static_lease *cur;
20	struct static_lease *new_static_lease;
21
22	/* Build new node */
23	new_static_lease = xmalloc(sizeof(struct static_lease));
24	new_static_lease->mac = mac;
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		cur = *lease_struct;
33		while (cur->next) {
34			cur = cur->next;
35		}
36
37		cur->next = new_static_lease;
38	}
39
40	return 1;
41}
42
43/* Check to see if a mac has an associated static lease */
44uint32_t getIpByMac(struct static_lease *lease_struct, void *arg)
45{
46	uint32_t return_ip;
47	struct static_lease *cur = lease_struct;
48	uint8_t *mac = arg;
49
50	return_ip = 0;
51
52	while (cur) {
53		/* If the client has the correct mac  */
54		if (memcmp(cur->mac, mac, 6) == 0) {
55			return_ip = *(cur->ip);
56		}
57
58		cur = cur->next;
59	}
60
61	return return_ip;
62}
63
64/* Check to see if an ip is reserved as a static ip */
65uint32_t reservedIp(struct static_lease *lease_struct, uint32_t ip)
66{
67	struct static_lease *cur = lease_struct;
68
69	uint32_t return_val = 0;
70
71	while (cur) {
72		/* If the client has the correct ip  */
73		if (*cur->ip == ip)
74			return_val = 1;
75
76		cur = cur->next;
77	}
78
79	return return_val;
80}
81
82#if ENABLE_FEATURE_UDHCP_DEBUG
83/* Print out static leases just to check what's going on */
84/* Takes the address of the pointer to the static_leases linked list */
85void printStaticLeases(struct static_lease **arg)
86{
87	/* Get a pointer to the linked list */
88	struct static_lease *cur = *arg;
89
90	while (cur) {
91		/* printf("PrintStaticLeases: Lease mac Address: %x\n", cur->mac); */
92		printf("PrintStaticLeases: Lease mac Value: %x\n", *(cur->mac));
93		/* printf("PrintStaticLeases: Lease ip Address: %x\n", cur->ip); */
94		printf("PrintStaticLeases: Lease ip Value: %x\n", *(cur->ip));
95
96		cur = cur->next;
97	}
98}
99#endif
100