Skip to content

Commit

Permalink
Merge pull request #1703 from pi-hole/fix/delete_dhcp_lease
Browse files Browse the repository at this point in the history
Managing DHCP leases is only possible when the DHCP server is enabled
  • Loading branch information
DL6ER authored Jan 11, 2024
2 parents 02962d9 + e24c363 commit b0c2ab9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/api/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int api_dhcp_leases_GET(struct ftl_conn *api)
}

// defined in dnsmasq_interface.c
extern bool FTL_unlink_DHCP_lease(const char *ipaddr);
extern bool FTL_unlink_DHCP_lease(const char *ipaddr, const char **hint);

// Delete DHCP leases
int api_dhcp_leases_DELETE(struct ftl_conn *api)
Expand All @@ -92,7 +92,18 @@ int api_dhcp_leases_DELETE(struct ftl_conn *api)

// Delete lease
log_debug(DEBUG_API, "Deleting DHCP lease for address %s", api->item);
const bool found = FTL_unlink_DHCP_lease(api->item);

const char *hint = NULL;
const bool found = FTL_unlink_DHCP_lease(api->item, &hint);
if(!found && hint != NULL)
{
// Send error when something went wrong (hint is not NULL)
return send_json_error(api,
400,
"bad_request",
"Failed to delete DHCP lease",
hint);
}

// Send empty reply with codes:
// - 204 No Content (if a lease was deleted)
Expand Down
1 change: 1 addition & 0 deletions src/api/docs/content/specs/dhcp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ components:
operationId: "delete_dhcp"
description: |
This API hook removes a currently active DHCP lease.
Managing DHCP leases is only possible when the DHCP server is enabled.
*Note:* There will be no content on success.
responses:
'204':
Expand Down
16 changes: 14 additions & 2 deletions src/dnsmasq_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -3244,12 +3244,18 @@ void FTL_TCP_worker_created(const int confd)
gravityDB_forked();
}

bool FTL_unlink_DHCP_lease(const char *ipaddr)
bool FTL_unlink_DHCP_lease(const char *ipaddr, const char **hint)
{
struct dhcp_lease *lease;
union all_addr addr;
const time_t now = dnsmasq_time();

if(!daemon->dhcp)
{
*hint = "DHCP is not enabled";
return false;
}

// Try to extract IP address
if (inet_pton(AF_INET, ipaddr, &addr.addr4) > 0)
{
Expand All @@ -3263,7 +3269,8 @@ bool FTL_unlink_DHCP_lease(const char *ipaddr)
#endif
else
{
// Invalid IP address or no lease found
// Invalid IP address
*hint = "invalid target address (neither IPv4 nor IPv6)";
return false;
}

Expand All @@ -3280,6 +3287,11 @@ bool FTL_unlink_DHCP_lease(const char *ipaddr)
// (variable lease.c:dns_dirty is used here)
lease_update_dns(0);
}
else
{
*hint = NULL;
return false;
}

// Return success
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/dnsmasq_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void FTL_dnsmasq_reload(void);
void FTL_TCP_worker_created(const int confd);
void FTL_TCP_worker_terminating(bool finished);

bool FTL_unlink_DHCP_lease(const char *ipaddr);
bool FTL_unlink_DHCP_lease(const char *ipaddr, const char **hint);

// defined in src/dnsmasq/cache.c
extern char *querystr(char *desc, unsigned short type);
Expand Down

0 comments on commit b0c2ab9

Please sign in to comment.