"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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 | * |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 11 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 12 | #include "dhcpd.h" |
| 13 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 14 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 15 | /* Takes the address of the pointer to the static_leases linked list, |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 16 | * address to a 6 byte mac address, |
| 17 | * 4 byte IP address */ |
| 18 | void FAST_FUNC add_static_lease(struct static_lease **st_lease_pp, |
| 19 | uint8_t *mac, |
| 20 | uint32_t nip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 21 | { |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 22 | struct static_lease *st_lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 23 | |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 24 | /* Find the tail of the list */ |
| 25 | while ((st_lease = *st_lease_pp) != NULL) { |
| 26 | st_lease_pp = &st_lease->next; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | } |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 28 | |
| 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 Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 36 | /* Find static lease IP by mac */ |
| 37 | uint32_t FAST_FUNC get_static_nip_by_mac(struct static_lease *st_lease, void *mac) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 38 | { |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 39 | while (st_lease) { |
| 40 | if (memcmp(st_lease->mac, mac, 6) == 0) |
| 41 | return st_lease->nip; |
| 42 | st_lease = st_lease->next; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 45 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 48 | /* Check to see if an IP is reserved as a static IP */ |
| 49 | int FAST_FUNC is_nip_reserved(struct static_lease *st_lease, uint32_t nip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 50 | { |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 51 | while (st_lease) { |
| 52 | if (st_lease->nip == nip) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 53 | return 1; |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 54 | st_lease = st_lease->next; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 57 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Denys Vlasenko | 799d00e | 2009-06-17 13:44:04 +0200 | [diff] [blame^] | 60 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 61 | /* 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 Vlasenko | 799d00e | 2009-06-17 13:44:04 +0200 | [diff] [blame^] | 63 | void FAST_FUNC log_static_leases(struct static_lease **st_lease_pp) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 64 | { |
Denys Vlasenko | 799d00e | 2009-06-17 13:44:04 +0200 | [diff] [blame^] | 65 | struct static_lease *cur; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 66 | |
Denys Vlasenko | 799d00e | 2009-06-17 13:44:04 +0200 | [diff] [blame^] | 67 | if (dhcp_verbose < 2) |
| 68 | return; |
| 69 | |
| 70 | cur = *st_lease_pp; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 71 | while (cur) { |
Denys Vlasenko | 799d00e | 2009-06-17 13:44:04 +0200 | [diff] [blame^] | 72 | bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 73 | cur->mac[0], cur->mac[1], cur->mac[2], |
Denys Vlasenko | 799d00e | 2009-06-17 13:44:04 +0200 | [diff] [blame^] | 74 | cur->mac[3], cur->mac[4], cur->mac[5], |
| 75 | cur->nip |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 76 | ); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 77 | cur = cur->next; |
| 78 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 79 | } |
| 80 | #endif |