dhcp-server: Fix debug messages in send_offer
PATCH 1/3 dhcp-server: Fix debug messages in send_offer
find_free_or_expired_ip() or check_requested_ip() should fail if no addresses are left in the pool, rather than lease_add() so emit that error message there. The only thing lease_add() validates that has not been validated before might be the origin MAC, treat the lease_add() failure more as an internal error.
--- ell/dhcp-server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ell/dhcp-server.c +2 -2
@@ -451,13 +451,13 @@ static void send_offer(struct l_dhcp_server *server, client_msg->chaddr);if (!reply->yiaddr) { -SERVER_DEBUG ("Could not find lease or send offer" ); +SERVER_DEBUG ("No free IP addresses, OFFER abandoned" );return ; } lease =add_lease (server, true, client_msg->chaddr, reply->yiaddr);if (!lease) { -SERVER_DEBUG ("No free IP addresses, OFFER abandoned" ); +SERVER_DEBUG ("add_lease() failed" );return ; }
Signature · 1 line
2.30.2
PATCH 2/3 dhcp-server: Fill in netmask & gateway in lease objects
When creating new l_dhcp_lease objects for client leases, also fill in the netmask and the gateway fields for the benefit of the users of those lease objects outside dhcp-server.c.
--- ell/dhcp-server.c | 2 ++ 1 file changed, 2 insertions(+)
ell/dhcp-server.c +2 -0
@@ -262,6 +262,8 @@ static struct l_dhcp_lease *add_lease(struct l_dhcp_server *server,memcpy (lease->mac, chaddr,ETH_ALEN ); lease->address = yiaddr; + lease->subnet_mask = server->netmask; + lease->router = server->gateway; lease->offering = offering; lease->bound_time =l_time_now ();
Signature · 1 line
2.30.2
PATCH 3/3 dhcp-server: Add l_dhcp_server_{new,release}_lease
In reply to Andrew ZaborowskiAdd API for obtaining and releasing DHCP leases directly, as if by sending the appropriate DISCOVER, REQUEST and RELEASE messages but with just method calls. This is useful for implementing mechanisms like the optional 802.11 4-Way Handshake IP Allocation (reduces the number of roundtrips in a P2P connection setup).
--- ell/dhcp-server.c | 52 +++++++++++++++++++++++++++++++++++++++++++++-- ell/dhcp.h | 5 +++++ ell/ell.sym | 2 ++ 3 files changed, 57 insertions(+), 2 deletions(-)
ell/dhcp-server.c +50 -2
@@ -129,12 +129,14 @@ static struct l_dhcp_lease *find_lease_by_mac(struct l_dhcp_server *server,return l_queue_find (server->lease_list, match_lease_mac, mac); } -static void remove_lease (struct l_dhcp_server *server, +static bool remove_lease (struct l_dhcp_server *server,struct l_dhcp_lease *lease) { -l_queue_remove (server->lease_list, lease); +if (!l_queue_remove (server->lease_list, lease)) +return false;_dhcp_lease_free (lease); +return true; }/* Clear the old lease and create the new one */
@@ -1043,3 +1045,49 @@ failed:l_free (dns_list);return false; } + +/* +* Create a new lease, like by sending a DISCOVER and a REQUEST. The lease +* stays valid until the server is destroyed, l_dhcp_server_release_lease is +* called or L_DHCP_SERVER_EVENT_LEASE_EXPIRED is emitted for it. +*/ +LIB_EXPORT struct l_dhcp_lease *l_dhcp_server_new_lease( +struct l_dhcp_server *server, +const uint8_t *mac) +{ +struct l_dhcp_lease *lease; +uint32_t ip =find_free_or_expired_ip (server, mac); + +if (!ip) { +SERVER_DEBUG ("Could not find any free addresses" ); +return NULL ; + } + + lease =add_lease (server, false, mac, ip); +if (!lease) { +SERVER_DEBUG ("add_lease() failed" ); +return NULL ; + } + +SERVER_DEBUG ("Allocated %s to " MAC ,IP_STR (ip),MAC_STR (mac)); + +if (server->event_handler) + server->event_handler (server,L_DHCP_SERVER_EVENT_NEW_LEASE , + server->user_data, lease); + +return lease; +} + +LIB_EXPORT bool l_dhcp_server_release_lease(struct l_dhcp_server *server, +struct l_dhcp_lease *lease) +{ +/* +* Don't emit event or put the lease in the expired list because +* the lease is not expiring. +*/ +if (!remove_lease (server, lease)) +return false; + +set_next_expire_timer (server,NULL ); +return true; +}
ell/dhcp.h +5 -0
@@ -141,6 +141,11 @@ bool l_dhcp_server_set_ip_address(struct l_dhcp_server *server,bool l_dhcp_server_set_netmask (struct l_dhcp_server *server,const char *mask);bool l_dhcp_server_set_gateway (struct l_dhcp_server *server,const char *ip);bool l_dhcp_server_set_dns (struct l_dhcp_server *server,char **dns); + +struct l_dhcp_lease *l_dhcp_server_new_lease (struct l_dhcp_server *server, +const uint8_t *mac); +bool l_dhcp_server_release_lease (struct l_dhcp_server *server, +struct l_dhcp_lease *lease); #ifdef __cplusplus }#endif
ell/ell.sym +2 -0
@@ -259,6 +259,8 @@ global: l_dhcp_server_set_netmask; l_dhcp_server_set_gateway; l_dhcp_server_set_dns; + l_dhcp_server_new_lease; + l_dhcp_server_release_lease; /* dhcp6 */ l_dhcp6_client_new; l_dhcp6_client_destroy;
Signature · 1 line
2.30.2
Hi Andrew,
Quoted 9 lines · Andrew Zaborowski
find_free_or_expired_ip()orcheck_requested_ip()should fail if no addresses are left in the pool, rather thanlease_add()so emit that error message there. The only thinglease_add()validates that has not been validated before might be the origin MAC, treat thelease_add()failure more as an internal error.--- ell/dhcp-server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
All applied, thanks.
Regards,
-Denis
Hi Andrew,
Quoted 13 lines · Denis Kenzior
Hi Andrew,
Quoted 9 lines · Andrew Zaborowski
find_free_or_expired_ip()orcheck_requested_ip()should fail if no addresses are left in the pool, rather thanlease_add()so emit that error message there. The only thinglease_add()validates that has not been validated before might be the origin MAC, treat thelease_add()failure more as an internal error.
---
ell/dhcp-server.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)
Actually, I pulled patch 3 back. It doesn't compile:
CC ell/dhcp-server.loell/dhcp-server.c: In function ‘l_dhcp_server_new_lease’:ell/dhcp-server.c:1071:39: error: implicit declaration of function ‘IP_STR’; did you mean ‘IP_TTL’? [-Werror=implicit-function-declaration]
1071 | SERVER_DEBUG("Allocated %s to " MAC, IP_STR(ip), MAC_STR(mac));
| ^~~~~~ell/dhcp-server.c:103:41: note: in definition of macro ‘SERVER_DEBUG’
103 | "%s:%i " fmt, __func__, __LINE__, ## args)
| ^~~~ell/dhcp-server.c:103:4: error: format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘int’ [-Werror=format=]
103 | "%s:%i " fmt, __func__, __LINE__, ## args)
| ^~~~~~~~
......
1071 | SERVER_DEBUG("Allocated %s to " MAC, IP_STR(ip), MAC_STR(mac));
| ~~~~~~~~~~
| |
| int
ell/dhcp-server.c:1071:2: note: in expansion of macro ‘SERVER_DEBUG’
1071 | SERVER_DEBUG("Allocated %s to " MAC, IP_STR(ip), MAC_STR(mac));
| ^~~~~~~~~~~~Regards,
-Denis