udhcp: cleanup of static lease handling
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
56f2d06c64
commit
cab3a0127c
@ -166,7 +166,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Look for a static lease */
|
/* Look for a static lease */
|
||||||
static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
|
static_lease_ip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
|
||||||
if (static_lease_ip) {
|
if (static_lease_ip) {
|
||||||
bb_info_msg("Found static lease: %x", static_lease_ip);
|
bb_info_msg("Found static lease: %x", static_lease_ip);
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ struct option_set {
|
|||||||
|
|
||||||
struct static_lease {
|
struct static_lease {
|
||||||
struct static_lease *next;
|
struct static_lease *next;
|
||||||
uint32_t ip;
|
uint32_t nip;
|
||||||
uint8_t mac[6];
|
uint8_t mac[6];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -97,15 +97,15 @@ uint32_t find_free_or_expired_address(const uint8_t *chaddr) FAST_FUNC;
|
|||||||
|
|
||||||
/*** static_leases.h ***/
|
/*** static_leases.h ***/
|
||||||
|
|
||||||
/* Config file will pass static lease info to this function which will add it
|
/* Config file parser will pass static lease info to this function
|
||||||
* to a data structure that can be searched later */
|
* which will add it to a data structure that can be searched later */
|
||||||
void addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t ip) FAST_FUNC;
|
void add_static_lease(struct static_lease **st_lease_pp, uint8_t *mac, uint32_t nip) FAST_FUNC;
|
||||||
/* Check to see if a mac has an associated static lease */
|
/* Find static lease IP by mac */
|
||||||
uint32_t getIpByMac(struct static_lease *lease_struct, void *arg) FAST_FUNC;
|
uint32_t get_static_nip_by_mac(struct static_lease *st_lease, void *arg) FAST_FUNC;
|
||||||
/* Check to see if an ip is reserved as a static ip */
|
/* Check to see if an IP is reserved as a static IP */
|
||||||
int reservedIp(struct static_lease *lease_struct, uint32_t ip) FAST_FUNC;
|
int is_nip_reserved(struct static_lease *st_lease, uint32_t nip) FAST_FUNC;
|
||||||
/* Print out static leases just to check what's going on (debug code) */
|
/* Print out static leases just to check what's going on (debug code) */
|
||||||
void printStaticLeases(struct static_lease **lease_struct) FAST_FUNC;
|
void print_static_leases(struct static_lease **st_lease_pp) FAST_FUNC;
|
||||||
|
|
||||||
|
|
||||||
/*** serverpacket.h ***/
|
/*** serverpacket.h ***/
|
||||||
|
@ -268,9 +268,9 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
|
|||||||
ip_string = strtok_r(NULL, " \t", &line);
|
ip_string = strtok_r(NULL, " \t", &line);
|
||||||
read_nip(ip_string, &ip);
|
read_nip(ip_string, &ip);
|
||||||
|
|
||||||
addStaticLease(arg, (uint8_t*) &mac_bytes, ip);
|
add_static_lease(arg, (uint8_t*) &mac_bytes, ip);
|
||||||
|
|
||||||
if (ENABLE_UDHCP_DEBUG) printStaticLeases(arg);
|
if (ENABLE_UDHCP_DEBUG) print_static_leases(arg);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -159,8 +159,8 @@ uint32_t FAST_FUNC find_free_or_expired_address(const uint8_t *chaddr)
|
|||||||
if ((addr & 0xff) == 0xff)
|
if ((addr & 0xff) == 0xff)
|
||||||
continue;
|
continue;
|
||||||
net_addr = htonl(addr);
|
net_addr = htonl(addr);
|
||||||
/* addr has a static lease? */
|
/* is this a static lease addr? */
|
||||||
if (reservedIp(server_config.static_leases, net_addr))
|
if (is_nip_reserved(server_config.static_leases, net_addr))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
lease = find_lease_by_yiaddr(net_addr);
|
lease = find_lease_by_yiaddr(net_addr);
|
||||||
|
@ -120,7 +120,7 @@ int FAST_FUNC send_offer(struct dhcpMessage *oldpacket)
|
|||||||
|
|
||||||
init_packet(&packet, oldpacket, DHCPOFFER);
|
init_packet(&packet, oldpacket, DHCPOFFER);
|
||||||
|
|
||||||
static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
|
static_lease_ip = get_static_nip_by_mac(server_config.static_leases, oldpacket->chaddr);
|
||||||
|
|
||||||
/* ADDME: if static, short circuit */
|
/* ADDME: if static, short circuit */
|
||||||
if (!static_lease_ip) {
|
if (!static_lease_ip) {
|
||||||
|
@ -13,48 +13,45 @@
|
|||||||
|
|
||||||
|
|
||||||
/* Takes the address of the pointer to the static_leases linked list,
|
/* Takes the address of the pointer to the static_leases linked list,
|
||||||
* Address to a 6 byte mac address
|
* address to a 6 byte mac address,
|
||||||
* Address to a 4 byte ip address */
|
* 4 byte IP address */
|
||||||
void FAST_FUNC addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t ip)
|
void FAST_FUNC add_static_lease(struct static_lease **st_lease_pp,
|
||||||
|
uint8_t *mac,
|
||||||
|
uint32_t nip)
|
||||||
{
|
{
|
||||||
struct static_lease *new_static_lease;
|
struct static_lease *st_lease;
|
||||||
|
|
||||||
/* Build new node */
|
/* Find the tail of the list */
|
||||||
new_static_lease = xzalloc(sizeof(struct static_lease));
|
while ((st_lease = *st_lease_pp) != NULL) {
|
||||||
memcpy(new_static_lease->mac, mac, 6);
|
st_lease_pp = &st_lease->next;
|
||||||
new_static_lease->ip = ip;
|
|
||||||
/*new_static_lease->next = NULL;*/
|
|
||||||
|
|
||||||
/* If it's the first node to be added... */
|
|
||||||
if (*lease_struct == NULL) {
|
|
||||||
*lease_struct = new_static_lease;
|
|
||||||
} else {
|
|
||||||
struct static_lease *cur = *lease_struct;
|
|
||||||
while (cur->next)
|
|
||||||
cur = cur->next;
|
|
||||||
cur->next = new_static_lease;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Add new node */
|
||||||
|
*st_lease_pp = st_lease = xzalloc(sizeof(*st_lease));
|
||||||
|
memcpy(st_lease->mac, mac, 6);
|
||||||
|
st_lease->nip = nip;
|
||||||
|
/*st_lease->next = NULL;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check to see if a mac has an associated static lease */
|
/* Find static lease IP by mac */
|
||||||
uint32_t FAST_FUNC getIpByMac(struct static_lease *lease_struct, void *mac)
|
uint32_t FAST_FUNC get_static_nip_by_mac(struct static_lease *st_lease, void *mac)
|
||||||
{
|
{
|
||||||
while (lease_struct) {
|
while (st_lease) {
|
||||||
if (memcmp(lease_struct->mac, mac, 6) == 0)
|
if (memcmp(st_lease->mac, mac, 6) == 0)
|
||||||
return lease_struct->ip;
|
return st_lease->nip;
|
||||||
lease_struct = lease_struct->next;
|
st_lease = st_lease->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check to see if an ip is reserved as a static ip */
|
/* Check to see if an IP is reserved as a static IP */
|
||||||
int FAST_FUNC reservedIp(struct static_lease *lease_struct, uint32_t ip)
|
int FAST_FUNC is_nip_reserved(struct static_lease *st_lease, uint32_t nip)
|
||||||
{
|
{
|
||||||
while (lease_struct) {
|
while (st_lease) {
|
||||||
if (lease_struct->ip == ip)
|
if (st_lease->nip == nip)
|
||||||
return 1;
|
return 1;
|
||||||
lease_struct = lease_struct->next;
|
st_lease = st_lease->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -63,16 +60,16 @@ int FAST_FUNC reservedIp(struct static_lease *lease_struct, uint32_t ip)
|
|||||||
#if ENABLE_UDHCP_DEBUG
|
#if ENABLE_UDHCP_DEBUG
|
||||||
/* Print out static leases just to check what's going on */
|
/* Print out static leases just to check what's going on */
|
||||||
/* Takes the address of the pointer to the static_leases linked list */
|
/* Takes the address of the pointer to the static_leases linked list */
|
||||||
void FAST_FUNC printStaticLeases(struct static_lease **arg)
|
void FAST_FUNC print_static_leases(struct static_lease **st_lease_pp)
|
||||||
{
|
{
|
||||||
struct static_lease *cur = *arg;
|
struct static_lease *cur = *st_lease_pp;
|
||||||
|
|
||||||
while (cur) {
|
while (cur) {
|
||||||
printf("PrintStaticLeases: Lease mac Value: %02x:%02x:%02x:%02x:%02x:%02x\n",
|
printf("PrintStaticLeases: lease mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||||
cur->mac[0], cur->mac[1], cur->mac[2],
|
cur->mac[0], cur->mac[1], cur->mac[2],
|
||||||
cur->mac[3], cur->mac[4], cur->mac[5]
|
cur->mac[3], cur->mac[4], cur->mac[5]
|
||||||
);
|
);
|
||||||
printf("PrintStaticLeases: Lease ip Value: %x\n", cur->ip);
|
printf("PrintStaticLeases: lease ip: %x\n", cur->nip);
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user