Hi.

I know that Kea stores leases for reservations unlike ISC DHCPd did.  I've run 
into a few issues where we replace a lab of machines with new machines, and 
it's not easy to remove the old ether information from the kea lease db.   I 
have a tool which lists the lease with lease4-get-by-hostname, then clears the 
lease with lease4-del, but it just doesn't always seem to work.   I see the 
lease there, I clear it, then lease4-get-by-hostname can't find a lease 
anymore, but Kea won't give out the reserved IP because it think its in use.

I really like the old behaviour of NOT recording lease information for 
reservations with static IPs.  Anyway... I had a thought... What if with the 
hook system, I could make this work with Kea.  I don't know anything about Kea 
API development, but I asked a friend of mine (GPT!!) if he had any 
suggestions.  This is what he wrote for me:

In essence, it should do exactly what I want...

The LOG_INFO lines were commented because they produce errors.
There are issues with getSubnet4 and getClientId calls...

So ... I'm just wondering if there's anyone familiar with Kea hook development 
who might be able to look at what ChatGPT created for me, and make the fixes 
(if they are simple to make) that would make this compile.   On the surface, it 
looks like it would work but this could be all wrong.


#include <hooks/hooks.h>
#include <hooks/hooks_log.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/host_mgr.h>
#include <dhcp/pkt4.h>
#include <dhcpsrv/lease.h>
#include <log/logger_support.h>

using namespace isc::hooks;
using namespace isc::dhcp;
using namespace isc::log;

extern "C" {

    int load(LibraryHandle& handle) {
        //LOG_INFO(hooks_logger, DHCP4_HOOKS_LEASE4_SELECT).arg("Hook loaded");
        return (0);
    }

    int unload() {
        return (0);
    }

    // The hook for lease4_select (for IPv4)
    // This is called when Kea selects a lease for an incoming request.
    Lease4Ptr lease4_select(CalloutHandle& handle) {
        // Retrieve the packet information (e.g., client MAC)
        Pkt4Ptr query;
        handle.getArgument("query4", query);

        if (!query) {
            return (Lease4Ptr()); // If the query is invalid, return an empty 
lease
        }

        // Retrieve the MAC address from the incoming packet
        HWAddrPtr mac = query->getHWAddr();

        if (!mac) {
            return (Lease4Ptr()); // If there's no MAC, return nothing
        }

        // Check if this MAC address has a reservation
        HostMgr& host_mgr = HostMgr::instance();
        const Subnet4* subnet = query->getSubnet4();
        if (!subnet) {
            return (Lease4Ptr());
        }

        HostPtr host = host_mgr.get4(subnet->getID(), mac, 
query->getClientId());

        if (host) {
            // A reservation exists for this MAC. Skip lease storage.
            //LOG_INFO(hooks_logger, DHCP4_HOOKS_LEASE4_SELECT).arg("Lease for 
reserved IP is skipped for host with MAC: ")
                                                            
.arg(mac->toText(false));
            return (Lease4Ptr());  // Returning null effectively skips lease 
recording.
        }

        return (Lease4Ptr()); // No reservation, let Kea handle it normally
    }

} // end extern "C"



Jason.










-- 
ISC funds the development of this software with paid support subscriptions. 
Contact us at https://www.isc.org/contact/ for more information.

To unsubscribe visit https://lists.isc.org/mailman/listinfo/kea-users.

Kea-users mailing list
[email protected]
https://lists.isc.org/mailman/listinfo/kea-users

Reply via email to