forked from sargon/ddhcpd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dhcp.c
693 lines (540 loc) · 20.9 KB
/
dhcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include "block.h"
#include "dhcp.h"
#include "dhcp_options.h"
#include "hook.h"
#include "logger.h"
#include "packet.h"
#include "statistics.h"
#include "tools.h"
// Free an offered lease after 12 seconds.
uint16_t DHCP_OFFER_TIMEOUT = 12;
uint16_t DHCP_LEASE_SERVER_DELTA = 10;
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
#define DEBUG_DHCP_LEASE(...) do { \
DEBUG("DHCP LEASE [ state %i, xid %u, end %i ]\n",lease->state,lease->xid,lease->lease_end);\
} while (0);
#else
#define DEBUG_LEASE(...)
#endif
/**
* Search for block and lease for given address, returns a status code and found
* results.
* A status code of 0 is returned, iff the result is in one of our blocks.
* Of 1, iff result is non in our blocks.
* And 2 on failure.
*/
ATTR_NONNULL(1,2) uint8_t find_lease_from_address(struct in_addr* addr, ddhcp_config* config, ddhcp_block** lease_block, uint32_t* lease_index) {
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
DEBUG("find_lease_from_address(%s, ...)\n", inet_ntoa(*addr));
#endif
ddhcp_block* blocks = config->blocks;
uint32_t address = (uint32_t) addr->s_addr;
uint32_t block_number = (ntohl(address) - ntohl((uint32_t) config->prefix.s_addr)) / config->block_size;
uint32_t lease_number = (ntohl(address) - ntohl((uint32_t) config->prefix.s_addr)) % config->block_size;
if (block_number < config->number_of_blocks) {
DEBUG("find_lease_from_address(...): found block %i and lease %i with state %i\n", block_number, lease_number, blocks[block_number].state);
if (lease_block) {
*lease_block = blocks + block_number;
}
if (lease_index) {
*lease_index = lease_number;
}
DEBUG("find_lease_from_address(...): state: %i\n", blocks[block_number].state);
if (blocks[block_number].state == DDHCP_OURS) {
return 0;
} else {
// TODO Try to acquire address for client
return 1;
}
}
DEBUG("find_lease_from_address(...): block index %i outside of configured network structure\n", block_number);
return 2;
}
ATTR_NONNULL_ALL static void _dhcp_release_lease(ddhcp_block* block, uint32_t lease_index) {
INFO("dhcp_release_lease(...): Releasing lease %i in block %i\n", lease_index, block->index);
dhcp_lease* lease = block->addresses + lease_index;
// TODO Should we really reset the chaddr or xid, RFC says we
// ''SHOULD retain a record of the client's initialization parameters for possible reuse''
memset(lease->chaddr, 0, 16);
lease->xid = 0;
lease->state = FREE;
}
ATTR_NONNULL_ALL dhcp_packet* build_initial_packet(dhcp_packet* from_client) {
DEBUG("build_initial_packet(from_client)\n");
dhcp_packet* packet = calloc(1, sizeof(dhcp_packet));
if (!packet) {
WARNING("build_initial_packet(...): packet memory allocation failed\n");
return NULL;
}
packet->op = 2;
packet->htype = from_client->htype;
packet->hlen = from_client->hlen;
packet->hops = from_client->hops;
packet->xid = from_client->xid;
packet->secs = 0;
packet->flags = from_client->flags;
memcpy(&packet->ciaddr, &from_client->ciaddr, 4);
// yiaddr
// siaddr
memcpy(&packet->giaddr, &from_client->giaddr, 4);
memcpy(&packet->chaddr, &from_client->chaddr, 16);
// sname
// file
// options
return packet;
}
uint8_t _ddo[1] = { 0 };
ATTR_NONNULL_ALL static int16_t _dhcp_default_options(uint8_t msg_type, dhcp_packet* packet, dhcp_packet* request, ddhcp_config* config, bool include_lease_time) {
int16_t num_options;
// TODO We need a more extendable way to build up options
// TODO Proper error handling
// Fill options list with requested options, allocate memory and reserve for additional
// dhcp options.
if ((num_options = fill_options(request->options, request->options_len, &config->options, 3, &packet->options)) < 0) {
return num_options;
}
packet->options_len = (uint8_t)num_options;
// DHCP Message Type
_ddo[0] = msg_type;
set_option(packet->options, packet->options_len, DHCP_CODE_MESSAGE_TYPE, 1, _ddo);
// To support DHCPINFORM (as of RFC 2132) we need to be able to omit the lease time
if (include_lease_time) {
// DHCP Lease Time
set_option_from_store(&config->options, packet->options, packet->options_len, DHCP_CODE_ADDRESS_LEASE_TIME);
}
// DHCP Server identifier
set_option_from_store(&config->options, packet->options, packet->options_len, DHCP_CODE_SERVER_IDENTIFIER);
return 0;
}
ATTR_NONNULL_ALL int dhcp_process(uint8_t* buffer, ssize_t len, ddhcp_config* config) {
// TODO Error Handling
struct dhcp_packet dhcp_packet_buf;
ssize_t ret = ntoh_dhcp_packet(&dhcp_packet_buf, buffer, len);
if (ret == 0) {
int message_type = dhcp_packet_message_type(&dhcp_packet_buf);
switch (message_type) {
case DHCPDISCOVER:
statistics_record(config, STAT_DHCP_RECV_DISCOVER, 1);
ret = dhcp_hdl_discover(DDHCP_SKT_DHCP(config)->fd, &dhcp_packet_buf, config);
if (ret == 1) {
INFO("dhcp_process(...): we need to inquire new blocks\n");
return 1;
}
break;
case DHCPREQUEST:
statistics_record(config, STAT_DHCP_RECV_REQUEST, 1);
dhcp_hdl_request(DDHCP_SKT_DHCP(config)->fd, &dhcp_packet_buf, config);
break;
case DHCPRELEASE:
statistics_record(config, STAT_DHCP_RECV_RELEASE, 1);
dhcp_hdl_release(&dhcp_packet_buf, config);
break;
case DHCPINFORM:
statistics_record(config, STAT_DHCP_RECV_INFORM, 1);
dhcp_hdl_inform(DDHCP_SKT_DHCP(config)->fd, &dhcp_packet_buf, config);
break;
default:
WARNING("dhcp_process(...): Unknown DHCP message of type %i\n", message_type);
break;
}
if (dhcp_packet_buf.options_len > 0) {
free(dhcp_packet_buf.options);
}
} else {
WARNING("dhcp_process(...): Malformed packet!? errcode: %li\n", ret);
}
return 0;
}
ATTR_NONNULL_ALL int dhcp_hdl_discover(int socket, dhcp_packet* discover, ddhcp_config* config) {
DEBUG("dhcp_hdl_discover(socket:%i, packet, config)\n", socket);
time_t now = time(NULL);
ddhcp_block* lease_block = block_find_free_leases(config);
if (!lease_block) {
DEBUG("dhcp_hdl_discover(...): no block with free leases found\n");
return 3;
}
uint32_t lease_index = dhcp_get_free_lease(lease_block);
dhcp_lease* lease = lease_block->addresses + lease_index;
if (!lease) {
DEBUG("dhcp_hdl_discover(...): no free leases found, this should not happen!\n");
return 2;
}
dhcp_packet* packet = build_initial_packet(discover);
if (!packet) {
WARNING("dhcp_hdl_discover(...): packet memory allocation failed\n");
return 1;
}
// Mark lease as offered and register client
memcpy(&lease->chaddr, &discover->chaddr, 16);
lease->xid = discover->xid;
lease->state = OFFERED;
lease->lease_end = now + DHCP_OFFER_TIMEOUT;
addr_add(&lease_block->subnet, &packet->yiaddr, (int)lease_index);
DEBUG("dhcp_hdl_discover(...): offering address %i %s\n", lease_index, inet_ntoa(lease_block->subnet));
if (_dhcp_default_options(DHCPOFFER, packet, discover, config, true)) {
WARNING("dhcp_hdl_discover(...): option memory allocation failed\n");
free(packet);
return 1;
}
statistics_record(config, STAT_DHCP_SEND_PKG, 1);
statistics_record(config, STAT_DHCP_SEND_OFFER, 1);
ssize_t bytes_send = dhcp_packet_send(socket, packet);
statistics_record(config, STAT_DHCP_SEND_BYTE, (long int) bytes_send);
if (bytes_send > 0) {
// We needed the block, hence remove a possible needless marking.
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
if ( lease_block->needless_since > 0 ) {
DEBUG("dhcp_hdl_discover(...): Reset needless marker for block %i\n",lease_block->index);
}
#endif
lease_block->needless_since = 0;
}
free(packet->options);
free(packet);
return 0;
}
ATTR_NONNULL_ALL int dhcp_rhdl_request(uint32_t* address, ddhcp_config* config) {
DEBUG("dhcp_rhdl_request(address,config)\n");
time_t now = time(NULL);
ddhcp_block* lease_block = NULL;
uint32_t lease_index = 0;
struct in_addr requested_address;
memcpy(&requested_address, address, sizeof(struct in_addr));
uint8_t found = find_lease_from_address(&requested_address, config, &lease_block, &lease_index);
if (found == 0) {
// Update lease information
// TODO Check for validity of request (chaddr)
dhcp_lease* lease = lease_block->addresses + lease_index;
lease->lease_end = now + find_in_option_store_address_lease_time(&config->options) + DHCP_LEASE_SERVER_DELTA;
// Report ack
return 0;
} else if (found == 1) {
// We got a request for a block we don't own (anymore?)
// Reply with a nack
return 1;
} else {
return 2;
}
}
ATTR_NONNULL_ALL int dhcp_rhdl_ack(int socket, struct dhcp_packet* request, ddhcp_config* config) {
ddhcp_block* lease_block = NULL;
uint32_t lease_index = 0;
struct in_addr requested_address = {0};
uint8_t* address = find_option_requested_address(request->options, request->options_len);
if (address) {
memcpy(&requested_address, address, sizeof(struct in_addr));
} else if (request->ciaddr.s_addr != INADDR_ANY) {
memcpy(&requested_address, &request->ciaddr.s_addr, sizeof(struct in_addr));
}
if (find_lease_from_address(&requested_address, config, &lease_block, &lease_index) != 1) {
DEBUG("dhcp_rhdl_ack(...): lease not found\n");
return 1;
}
return dhcp_ack(socket, request, lease_block, lease_index, config);
}
ATTR_NONNULL_ALL int dhcp_hdl_request(int socket, struct dhcp_packet* request, ddhcp_config* config) {
DEBUG("dhcp_hdl_request(socket:%i, dhcp_packet, config)\n", socket);
// search the lease we may have offered
time_t now = time(NULL);
dhcp_lease* lease = NULL ;
ddhcp_block* lease_block = NULL;
uint32_t lease_index = 0;
uint8_t* address = find_option_requested_address(request->options, request->options_len);
struct in_addr requested_address;
uint8_t found_address = 0;
if (address) {
memcpy(&requested_address, address, sizeof(struct in_addr));
found_address = 1;
} else if (request->ciaddr.s_addr != INADDR_ANY) {
memcpy(&requested_address, &request->ciaddr.s_addr, sizeof(struct in_addr));
found_address = 1;
}
if (found_address) {
// Calculate block and dhcp_lease from address
uint8_t found = find_lease_from_address(&requested_address, config, &lease_block, &lease_index);
if (found != 2) {
lease = lease_block->addresses + lease_index;
DEBUG("dhcp_hdl_request(...): Lease found.\n");
if (lease_block->state == DDHCP_CLAIMED) {
if (block_alloc(lease_block)) {
ERROR("dhcp_hdl_request(...): can't allocate requested block\n");
dhcp_nack(socket, request, config);
}
lease = lease_block->addresses + lease_index;
// This lease block is not ours so we have to forward the request
DEBUG("dhcp_hdl_request(...): Requested lease is owned by another node. Sent request.\n");
// Register client information in lease
// TODO This isn't a good idea, because of multi request on the same address from various clients, register it elsewhere and append xid.
lease->xid = request->xid;
lease->state = OFFERED;
lease->lease_end = now + find_in_option_store_address_lease_time(&config->options) + DHCP_LEASE_SERVER_DELTA;
memcpy(&lease->chaddr, &request->chaddr, 16);
// Build packet and send it
ddhcp_renew_payload payload;
memcpy(&payload.chaddr, &request->chaddr, 16);
memcpy(&payload.address, &requested_address, sizeof(struct in_addr));
payload.xid = request->xid;
payload.lease_seconds = 0;
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(payload.chaddr);
DEBUG("dhcp_hdl_request(...): Save request for xid: %u chaddr: %s\n", payload.xid, hwaddr);
free(hwaddr);
#endif
// Send packet
ddhcp_mcast_packet* packet = new_ddhcp_packet(DDHCP_MSG_RENEWLEASE, config);
if (!packet) {
WARNING("dhcp_hdl_request(...): Failed to allocate memory for ddhcpd mcast packet\n");
return -ENOMEM;
}
packet->renew_payload = &payload;
// Store packet for later usage.
// TODO Error handling
dhcp_packet_list_add(&config->dhcp_packet_cache, request);
statistics_record(config, STAT_DIRECT_SEND_PKG, 1);
statistics_record(config, STAT_DIRECT_SEND_RENEWLEASE, 1);
ssize_t bytes_send = send_packet_direct(packet, &lease_block->owner_address, DDHCP_SKT_SERVER(config));
statistics_record(config, STAT_DIRECT_SEND_BYTE, (long int) bytes_send);
UNUSED(bytes_send);
free(packet);
return 2;
} else if (lease_block->state == DDHCP_OURS) {
if (lease->state != OFFERED || lease->xid != request->xid) {
if (memcmp(request->chaddr, lease->chaddr, 16) != 0) {
// Check if lease is free
if (lease->state != FREE) {
DEBUG("dhcp_hdl_request(...): Requested lease offered to other client\n");
// Send DHCP_NACK
dhcp_nack(socket, request, config);
return 2;
}
}
}
} else {
// Block is neither blocked nor ours, so probably say nak here
if ( block_num_owned(config) > 0 ) {
// When we own a block the learning phase is over, so we can safely nack requests
// in this step.
dhcp_nack(socket, request, config);
} else {
INFO("dhcp_hdl_request(...): Request could not be nacked safely.\n");
}
return 2;
}
}
} else {
ddhcp_block* block = config->blocks;
// Find lease from xid
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_OURS) {
dhcp_lease* lease_iter = block->addresses;
for (unsigned int j = 0 ; j < block->subnet_len ; j++) {
if (lease_iter->state == OFFERED && lease_iter->xid == request->xid) {
if (memcmp(request->chaddr, lease_iter->chaddr, 16) == 0) {
DEBUG("dhcp_hdl_request(...): Found requested lease\n");
lease = lease_iter;
lease_block = block;
lease_index = j;
break;
}
}
lease_iter++;
}
if (lease) {
break;
}
}
block++;
}
}
if (!lease) {
DEBUG("dhcp_hdl_request(...): Requested lease not found\n");
// Send DHCP_NACK
dhcp_nack(socket, request, config);
return 2;
}
return dhcp_ack(socket, request, lease_block, lease_index, config);
}
ATTR_NONNULL_ALL void dhcp_hdl_release(dhcp_packet* packet, ddhcp_config* config) {
DEBUG("dhcp_hdl_release(dhcp_packet,config)\n");
ddhcp_block* lease_block = NULL;
uint32_t lease_index = 0;
struct in_addr addr;
memcpy(&addr, &packet->ciaddr, sizeof(struct in_addr));
uint8_t found = find_lease_from_address(&addr, config, &lease_block, &lease_index);
dhcp_lease* lease;
switch (found) {
case 0:
lease = lease_block->addresses + lease_index;
// Check Hardware Address of client
if (memcmp(packet->chaddr, lease->chaddr, 16) == 0) {
_dhcp_release_lease(lease_block, lease_index);
hook_address(HOOK_RELEASE, &packet->yiaddr, (uint8_t*) &packet->chaddr, config);
} else {
ERROR("dhcp_hdl_release(...): Hardware address transmitted by client did not match with our record, doing nothing.\n");
}
case 1:
// TODO Handle remote block
// Send Message to neighbor
break;
default:
// Since there is no reply to this message, we could `silently` drop this case.
break;
}
}
ATTR_NONNULL_ALL void dhcp_hdl_inform(int socket, dhcp_packet* request, ddhcp_config* config) {
DEBUG("dhcp_hdl_inform(socket:%i, dhcp_packet,config)\n", socket);
ddhcp_block* lease_block = NULL;
uint32_t lease_index = 0;
struct in_addr addr;
memcpy(&addr, &request->ciaddr, sizeof(struct in_addr));
find_lease_from_address(&addr, config, &lease_block, &lease_index);
dhcp_packet* packet = build_initial_packet(request);
if (!packet) {
WARNING("dhcp_hdl_inform(...): packed memory allocation failed\n");
return;
}
if (_dhcp_default_options(DHCPACK, packet, request, config, false)) {
WARNING("dhcp_hdl_inform(...): option memory allocation failed\n");
free(packet);
return;
}
DEBUG("dhcp_hdl_inform(...): informing address %i %s\n", lease_index, inet_ntoa(packet->ciaddr));
statistics_record(config, STAT_DHCP_SEND_PKG, 1);
statistics_record(config, STAT_DHCP_SEND_ACK, 1);
ssize_t bytes_send = dhcp_packet_send(socket, packet);
statistics_record(config, STAT_DHCP_SEND_BYTE, (long int) bytes_send);
UNUSED(bytes_send);
hook_address(HOOK_INFORM, &packet->ciaddr, (uint8_t*) &packet->chaddr, config);
free(packet->options);
free(packet);
}
ATTR_NONNULL_ALL int dhcp_nack(int socket, dhcp_packet* from_client, ddhcp_config* config) {
dhcp_packet* packet = build_initial_packet(from_client);
if (!packet) {
WARNING("dhcp_nack(...): packet memory allocation failed\n");
return 1;
}
packet->options_len = 1;
packet->options = calloc(1, sizeof(dhcp_option));
if (!packet->options) {
WARNING("dhcp_nack(...): option memory allocation failed\n");
free(packet);
return 1;
}
set_option(packet->options, packet->options_len, DHCP_CODE_MESSAGE_TYPE, 1, (uint8_t[]) {
DHCPNAK
});
statistics_record(config, STAT_DHCP_SEND_PKG, 1);
statistics_record(config, STAT_DHCP_SEND_NAK, 1);
ssize_t bytes_send = dhcp_packet_send(socket, packet);
statistics_record(config, STAT_DHCP_SEND_BYTE, (long int) bytes_send);
UNUSED(bytes_send);
UNUSED(config);
free(packet->options);
free(packet);
return 0;
}
ATTR_NONNULL_ALL int dhcp_ack(int socket, dhcp_packet* request, ddhcp_block* lease_block, uint32_t lease_index, ddhcp_config* config) {
time_t now = time(NULL);
dhcp_lease* lease = lease_block->addresses + lease_index;
dhcp_packet* packet = build_initial_packet(request);
if (!packet) {
WARNING("dhcp_ack(...): packed memory allocation failed\n");
return 1;
}
// Mark lease as leased and register client
memcpy(&lease->chaddr, &request->chaddr, 16);
lease->xid = request->xid;
lease->state = LEASED;
lease->lease_end = now + find_in_option_store_address_lease_time(&config->options) + DHCP_LEASE_SERVER_DELTA;
addr_add(&lease_block->subnet, &packet->yiaddr, (int)lease_index);
if (_dhcp_default_options(DHCPACK, packet, request, config, true)) {
WARNING("dhcp_ack(...): option memory allocation failed\n");
free(packet);
return 1;
}
DEBUG("dhcp_ack(...): offering address %i %s\n", lease_index, inet_ntoa(packet->yiaddr));
statistics_record(config, STAT_DHCP_SEND_PKG, 1);
statistics_record(config, STAT_DHCP_SEND_ACK, 1);
ssize_t bytes_send = dhcp_packet_send(socket, packet);
statistics_record(config, STAT_DHCP_SEND_BYTE, (long int) bytes_send);
UNUSED(bytes_send);
hook_address(HOOK_LEASE, &packet->yiaddr, (uint8_t*) &packet->chaddr, config);
free(packet->options);
free(packet);
return 0;
}
ATTR_NONNULL_ALL int dhcp_has_free(struct ddhcp_block* block) {
dhcp_lease* lease = block->addresses;
for (unsigned int i = 0; i < block->subnet_len; i++) {
if (lease->state == FREE) {
return 1;
}
lease++;
}
return 0;
}
ATTR_NONNULL_ALL uint32_t dhcp_num_free(struct ddhcp_block* block) {
uint32_t num = 0;
dhcp_lease* lease = block->addresses;
for (unsigned int i = 0 ; i < block->subnet_len ; i++) {
if (lease->state == FREE) {
num++;
}
lease++;
}
return num;
}
ATTR_NONNULL_ALL uint32_t dhcp_num_offered(struct ddhcp_block* block) {
uint32_t num = 0;
dhcp_lease* lease = block->addresses;
for (unsigned int i = 0 ; i < block->subnet_len ; i++) {
if (lease->state == OFFERED) {
num++;
}
lease++;
}
return num;
}
ATTR_NONNULL_ALL uint32_t dhcp_get_free_lease(ddhcp_block* block) {
dhcp_lease* lease = block->addresses;
for (uint32_t i = 0 ; i < block->subnet_len ; i++) {
if (lease->state == FREE) {
return i;
}
lease++;
}
ERROR("dhcp_get_free_lease(...): no free leases found");
return block->subnet_len;
}
ATTR_NONNULL_ALL void dhcp_release_lease(uint32_t address, ddhcp_config* config) {
ddhcp_block* lease_block = NULL;
uint32_t lease_index = 0;
struct in_addr addr;
memcpy(&addr, &address, sizeof(struct in_addr));
uint8_t found = find_lease_from_address(&addr, config, &lease_block, &lease_index);
if (found == 0) {
_dhcp_release_lease(lease_block, lease_index);
} else {
DEBUG("dhcp_release_lease(...): No lease for address %s found.\n", inet_ntoa(addr));
}
}
ATTR_NONNULL_ALL int dhcp_check_timeouts(ddhcp_block* block) {
DEBUG("dhcp_check_timeouts(block)\n");
dhcp_lease* lease = block->addresses;
time_t now = time(NULL);
int free_leases = 0;
for (unsigned int i = 0 ; i < block->subnet_len ; i++) {
if (lease->state != FREE && lease->lease_end < now) {
_dhcp_release_lease(block, i);
}
if (lease->state == FREE) {
free_leases++;
}
lease++;
}
return free_leases;
}