blob: aabfb81aaad2f229cef5c0b2fdc60a2abc8536fc [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 *
8 */
9
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000010#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include "dhcpd.h"
12
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000013
Mike Frysinger7031f622006-05-08 03:20:50 +000014/* 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{
Mike Frysinger7031f622006-05-08 03:20:50 +000019 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... */
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 {
Mike Frysinger7031f622006-05-08 03:20:50 +000032 cur = *lease_struct;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000033 while (cur->next) {
Mike Frysinger7031f622006-05-08 03:20:50 +000034 cur = cur->next;
35 }
36
37 cur->next = new_static_lease;
38 }
39
40 return 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000041}
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
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000052 while (cur) {
Mike Frysinger7031f622006-05-08 03:20:50 +000053 /* If the client has the correct mac */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000054 if (memcmp(cur->mac, mac, 6) == 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +000055 return_ip = *(cur->ip);
56 }
57
58 cur = cur->next;
59 }
60
61 return return_ip;
Mike Frysinger7031f622006-05-08 03:20:50 +000062}
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
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000071 while (cur) {
Mike Frysinger7031f622006-05-08 03:20:50 +000072 /* If the client has the correct ip */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000073 if (*cur->ip == ip)
Mike Frysinger7031f622006-05-08 03:20:50 +000074 return_val = 1;
75
76 cur = cur->next;
77 }
78
79 return return_val;
Mike Frysinger7031f622006-05-08 03:20:50 +000080}
81
Denis Vlasenko04291bc2006-11-21 10:15:25 +000082#if ENABLE_FEATURE_UDHCP_DEBUG
Mike Frysinger7031f622006-05-08 03:20:50 +000083/* 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
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000090 while (cur) {
Mike Frysinger7031f622006-05-08 03:20:50 +000091 /* 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 }
Mike Frysinger7031f622006-05-08 03:20:50 +000098}
99#endif