Fix kbuild bugs noticed by Bernhard Fischer <rep.nop@aon.at>
[oweals/busybox.git] / networking / udhcp / static_leases.c
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
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "static_leases.h"
16 #include "dhcpd.h"
17
18 /* Takes the address of the pointer to the static_leases linked list,
19  *   Address to a 6 byte mac address
20  *   Address to a 4 byte ip address */
21 int addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip)
22 {
23
24         struct static_lease *cur;
25         struct static_lease *new_static_lease;
26
27         /* Build new node */
28         new_static_lease = xmalloc(sizeof(struct static_lease));
29         new_static_lease->mac = mac;
30         new_static_lease->ip = ip;
31         new_static_lease->next = NULL;
32
33         /* If it's the first node to be added... */
34         if(*lease_struct == NULL)
35         {
36                 *lease_struct = new_static_lease;
37         }
38         else
39         {
40                 cur = *lease_struct;
41                 while(cur->next != NULL)
42                 {
43                         cur = cur->next;
44                 }
45
46                 cur->next = new_static_lease;
47         }
48
49         return 1;
50
51 }
52
53 /* Check to see if a mac has an associated static lease */
54 uint32_t getIpByMac(struct static_lease *lease_struct, void *arg)
55 {
56         uint32_t return_ip;
57         struct static_lease *cur = lease_struct;
58         uint8_t *mac = arg;
59
60         return_ip = 0;
61
62         while(cur != NULL)
63         {
64                 /* If the client has the correct mac  */
65                 if(memcmp(cur->mac, mac, 6) == 0)
66                 {
67                         return_ip = *(cur->ip);
68                 }
69
70                 cur = cur->next;
71         }
72
73         return return_ip;
74
75 }
76
77 /* Check to see if an ip is reserved as a static ip */
78 uint32_t reservedIp(struct static_lease *lease_struct, uint32_t ip)
79 {
80         struct static_lease *cur = lease_struct;
81
82         uint32_t return_val = 0;
83
84         while(cur != NULL)
85         {
86                 /* If the client has the correct ip  */
87                 if(*cur->ip == ip)
88                         return_val = 1;
89
90                 cur = cur->next;
91         }
92
93         return return_val;
94
95 }
96
97 #ifdef CONFIG_FEATURE_UDHCP_DEBUG
98 /* Print out static leases just to check what's going on */
99 /* Takes the address of the pointer to the static_leases linked list */
100 void printStaticLeases(struct static_lease **arg)
101 {
102         /* Get a pointer to the linked list */
103         struct static_lease *cur = *arg;
104
105         while(cur != NULL)
106         {
107                 /* printf("PrintStaticLeases: Lease mac Address: %x\n", cur->mac); */
108                 printf("PrintStaticLeases: Lease mac Value: %x\n", *(cur->mac));
109                 /* printf("PrintStaticLeases: Lease ip Address: %x\n", cur->ip); */
110                 printf("PrintStaticLeases: Lease ip Value: %x\n", *(cur->ip));
111
112                 cur = cur->next;
113         }
114
115
116 }
117 #endif
118
119
120