"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 | * |
| 8 | */ |
| 9 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 10 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 11 | #include "dhcpd.h" |
| 12 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 13 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 14 | /* 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 */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 17 | int FAST_FUNC addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 18 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 19 | 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 Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 29 | if (*lease_struct == NULL) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 30 | *lease_struct = new_static_lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 31 | } else { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 32 | cur = *lease_struct; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 33 | while (cur->next) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | cur = cur->next; |
| 35 | } |
| 36 | |
| 37 | cur->next = new_static_lease; |
| 38 | } |
| 39 | |
| 40 | return 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* Check to see if a mac has an associated static lease */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 44 | uint32_t FAST_FUNC getIpByMac(struct static_lease *lease_struct, void *arg) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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 Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 52 | while (cur) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 53 | /* If the client has the correct mac */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 54 | if (memcmp(cur->mac, mac, 6) == 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 55 | return_ip = *(cur->ip); |
| 56 | } |
| 57 | |
| 58 | cur = cur->next; |
| 59 | } |
| 60 | |
| 61 | return return_ip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* Check to see if an ip is reserved as a static ip */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 65 | uint32_t FAST_FUNC reservedIp(struct static_lease *lease_struct, uint32_t ip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 66 | { |
| 67 | struct static_lease *cur = lease_struct; |
| 68 | |
| 69 | uint32_t return_val = 0; |
| 70 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 71 | while (cur) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 72 | /* If the client has the correct ip */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 73 | if (*cur->ip == ip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 74 | return_val = 1; |
| 75 | |
| 76 | cur = cur->next; |
| 77 | } |
| 78 | |
| 79 | return return_val; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 82 | #if ENABLE_UDHCP_DEBUG |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 83 | /* Print out static leases just to check what's going on */ |
| 84 | /* Takes the address of the pointer to the static_leases linked list */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 85 | void FAST_FUNC printStaticLeases(struct static_lease **arg) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 86 | { |
| 87 | /* Get a pointer to the linked list */ |
| 88 | struct static_lease *cur = *arg; |
| 89 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 90 | while (cur) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 91 | /* 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 Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 98 | } |
| 99 | #endif |