blob: 1887a8afdab6430ac22241eee9a70ae1476cf3a7 [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,
Denys Vlasenkocab3a012009-06-16 12:03:12 +020016 * address to a 6 byte mac address,
17 * 4 byte IP address */
18void FAST_FUNC add_static_lease(struct static_lease **st_lease_pp,
19 uint8_t *mac,
20 uint32_t nip)
Mike Frysinger7031f622006-05-08 03:20:50 +000021{
Denys Vlasenkocab3a012009-06-16 12:03:12 +020022 struct static_lease *st_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +000023
Denys Vlasenkocab3a012009-06-16 12:03:12 +020024 /* Find the tail of the list */
25 while ((st_lease = *st_lease_pp) != NULL) {
26 st_lease_pp = &st_lease->next;
Mike Frysinger7031f622006-05-08 03:20:50 +000027 }
Denys Vlasenkocab3a012009-06-16 12:03:12 +020028
29 /* Add new node */
30 *st_lease_pp = st_lease = xzalloc(sizeof(*st_lease));
31 memcpy(st_lease->mac, mac, 6);
32 st_lease->nip = nip;
33 /*st_lease->next = NULL;*/
Mike Frysinger7031f622006-05-08 03:20:50 +000034}
35
Denys Vlasenkocab3a012009-06-16 12:03:12 +020036/* Find static lease IP by mac */
37uint32_t FAST_FUNC get_static_nip_by_mac(struct static_lease *st_lease, void *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000038{
Denys Vlasenkocab3a012009-06-16 12:03:12 +020039 while (st_lease) {
40 if (memcmp(st_lease->mac, mac, 6) == 0)
41 return st_lease->nip;
42 st_lease = st_lease->next;
Mike Frysinger7031f622006-05-08 03:20:50 +000043 }
44
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000045 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000046}
47
Denys Vlasenkocab3a012009-06-16 12:03:12 +020048/* Check to see if an IP is reserved as a static IP */
49int FAST_FUNC is_nip_reserved(struct static_lease *st_lease, uint32_t nip)
Mike Frysinger7031f622006-05-08 03:20:50 +000050{
Denys Vlasenkocab3a012009-06-16 12:03:12 +020051 while (st_lease) {
52 if (st_lease->nip == nip)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000053 return 1;
Denys Vlasenkocab3a012009-06-16 12:03:12 +020054 st_lease = st_lease->next;
Mike Frysinger7031f622006-05-08 03:20:50 +000055 }
56
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000057 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000058}
59
Denys Vlasenko799d00e2009-06-17 13:44:04 +020060#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
Mike Frysinger7031f622006-05-08 03:20:50 +000061/* Print out static leases just to check what's going on */
62/* Takes the address of the pointer to the static_leases linked list */
Denys Vlasenko799d00e2009-06-17 13:44:04 +020063void FAST_FUNC log_static_leases(struct static_lease **st_lease_pp)
Mike Frysinger7031f622006-05-08 03:20:50 +000064{
Denys Vlasenko799d00e2009-06-17 13:44:04 +020065 struct static_lease *cur;
Mike Frysinger7031f622006-05-08 03:20:50 +000066
Denys Vlasenko799d00e2009-06-17 13:44:04 +020067 if (dhcp_verbose < 2)
68 return;
69
70 cur = *st_lease_pp;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000071 while (cur) {
Denys Vlasenko799d00e2009-06-17 13:44:04 +020072 bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x",
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000073 cur->mac[0], cur->mac[1], cur->mac[2],
Denys Vlasenko799d00e2009-06-17 13:44:04 +020074 cur->mac[3], cur->mac[4], cur->mac[5],
75 cur->nip
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000076 );
Mike Frysinger7031f622006-05-08 03:20:50 +000077 cur = cur->next;
78 }
Mike Frysinger7031f622006-05-08 03:20:50 +000079}
80#endif