int dhcpd_sendack(in_addr_t ipaddr) { uint32_t leasetime = CONFIG_NETUTILS_DHCPD_LEASETIME; in_addr_t netaddr; /* Initialize the ACK response */ dhcpd_initpacket(DHCPACK); memcpy(g_state.ds_outpacket.ciaddr, g_state.ds_inpacket.ciaddr, 4); /* Add the IP address assigned to the client */ netaddr = htonl(ipaddr); memcpy(g_state.ds_outpacket.yiaddr, &netaddr, 4); /* Did the client request a specific lease time? */ (void)dhcpd_verifyreqleasetime(&leasetime); /* Add the lease time to the response */ dhcpd_addoption32(DHCP_OPTION_LEASE_TIME, htonl(leasetime)); #ifdef CONFIG_NETUTILS_DHCPD_IGNOREBROADCAST if (dhcpd_sendpacket(true) < 0) #else if (dhcpd_sendpacket(false) < 0) #endif { return ERROR; } dhcpd_setlease(g_state.ds_inpacket.chaddr, ipaddr, leasetime); return OK; }
int dhcpd_sendack(in_addr_t ipaddr) { uint32_t leasetime = CONFIG_NETUTILS_DHCPD_LEASETIME; in_addr_t netaddr; #ifdef HAVE_DSNIP uint32_t dnsaddr; dnsaddr = htonl(CONFIG_NETUTILS_DHCPD_DNSIP); #endif /* Initialize the ACK response */ dhcpd_initpacket(DHCPACK); memcpy(g_state.ds_outpacket.ciaddr, g_state.ds_inpacket.ciaddr, 4); /* Add the IP address assigned to the client */ netaddr = htonl(ipaddr); memcpy(g_state.ds_outpacket.yiaddr, &netaddr, 4); /* Did the client request a specific lease time? */ (void)dhcpd_verifyreqleasetime(&leasetime); /* Add the lease time to the response */ dhcpd_addoption32(DHCP_OPTION_LEASE_TIME, htonl(leasetime)); #ifdef HAVE_NETMASK dhcpd_addoption32(DHCP_OPTION_SUBNET_MASK, htonl(CONFIG_NETUTILS_DHCPD_NETMASK)); #endif #ifdef HAVE_ROUTERIP dhcpd_addoption32(DHCP_OPTION_ROUTER, htonl(CONFIG_NETUTILS_DHCPD_ROUTERIP)); #endif #ifdef HAVE_DSNIP dhcp_addoption32p(DHCP_OPTION_DNS_SERVER, (FAR uint8_t*)&dnsaddr); #endif #ifdef CONFIG_NETUTILS_DHCPD_IGNOREBROADCAST if (dhcpd_sendpacket(true) < 0) #else if (dhcpd_sendpacket(false) < 0) #endif { return ERROR; } dhcpd_setlease(g_state.ds_inpacket.chaddr, ipaddr, leasetime); return OK; }
static inline int dhcpd_discover(void) { struct lease_s *lease; in_addr_t ipaddr; uint32_t leasetime = CONFIG_NETUTILS_DHCPD_LEASETIME; /* Check if the client is aleady in the lease table */ lease = dhcpd_findbymac(g_state.ds_inpacket.chaddr); if (lease) { /* Yes... get the remaining time on the lease */ #ifdef HAVE_LEASE_TIME if (!dhcpd_leaseexpired(lease)) { leasetime = lease->expiry - dhcpd_time(); if (leasetime < CONFIG_NETUTILS_DHCPD_MINLEASETIME) { leasetime = CONFIG_NETUTILS_DHCPD_MINLEASETIME; } } #endif /* Get the IP address associated with the lease (host order) */ ipaddr = dhcp_leaseipaddr(lease); nvdbg("Already have lease for IP %08lx\n", (long)ipaddr); } /* Check if the client has requested a specific IP address */ else if (dhcpd_verifyreqip()) { /* Use the requested IP address (host order) */ ipaddr = g_state.ds_optreqip; nvdbg("User requested IP %08lx\n", (long)ipaddr); } else { /* No... allocate a new IP address (host order)*/ ipaddr = dhcpd_allocipaddr(); nvdbg("Allocated IP %08lx\n", (long)ipaddr); } /* Did we get any IP address? */ if (!ipaddr) { /* Nope... return failure */ ndbg("Failed to get IP address\n"); return ERROR; } /* Reserve the leased IP for a shorter time for the offer */ if (!dhcpd_setlease(g_state.ds_inpacket.chaddr, ipaddr, CONFIG_NETUTILS_DHCPD_OFFERTIME)) { ndbg("Failed to set lease\n"); return ERROR; } /* Check if the client has requested a specific lease time */ (void)dhcpd_verifyreqleasetime(&leasetime); /* Send the offer response */ return dhcpd_sendoffer(ipaddr, leasetime); }