Lines Matching defs:dhcps
53 #include "lwip/dhcps.h"
83 struct dhcps {
94 #define dhcps_option_given(dhcps, idx) dhcp_option_given(&(dhcps)->dhcp, idx)
95 #define dhcps_got_option(dhcps, idx) dhcp_got_option(&(dhcps)->dhcp, idx)
96 #define dhcps_clear_option(dhcps, idx) dhcp_clear_option(&(dhcps)->dhcp, idx)
97 #define dhcps_clear_all_options(dhcps) dhcp_clear_all_options(&(dhcps)->dhcp)
98 #define dhcps_get_option_value(dhcps, idx) dhcp_get_option_value(&(dhcps)->dhcp, idx)
99 #define dhcps_set_option_value(dhcps, idx, val) dhcp_set_option_value(&(dhcps)->dhcp, idx, val)
101 #define netif_get_dhcps(netif) ((struct dhcps*)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCPS))
102 #define netif_set_dhcps(netif, dhcps) netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCPS, dhcps)
108 LWIP_STATIC err_t dhcps_parse_options(struct pbuf *p, struct dhcps *dhcps);
111 LWIP_STATIC void remove_stale_entries(struct dhcps *dhcps);
112 LWIP_STATIC void add_client_entry(struct dhcps *dhcps, unsigned int idx, struct dhcp_msg *client_msg);
113 LWIP_STATIC int find_free_slot(struct dhcps *dhcps);
114 LWIP_STATIC struct dyn_lease_addr *find_client_lease(struct dhcps *dhcps, struct dhcp_msg *client_msg);
115 LWIP_STATIC ip4_addr_t validate_discover(struct dhcps *dhcps, struct dhcp_msg *client_msg,
117 LWIP_STATIC void handle_discover(struct netif *netif, struct dhcps *dhcps, struct dhcp_msg *client_msg,
121 LWIP_STATIC void handle_request(struct netif *netif, struct dhcps *dhcps, struct dhcp_msg *client_msg,
123 LWIP_STATIC void handle_decline(struct netif *netif, struct dhcps *dhcps, struct dhcp_msg *client_msg,
125 LWIP_STATIC void handle_inform(struct netif *netif, struct dhcps *dhcps, struct dhcp_msg *client_msg);
126 LWIP_STATIC void handle_client_messages(struct netif *netif, struct dhcps *dhcps,
172 LWIP_STATIC void remove_stale_entries(struct dhcps *dhcps)
177 for (i = 0; i < dhcps->lease_num; i++) {
179 if ((dhcps->leasearr[i].flags != DHCPS_ADDRESS_FREE) && (dhcps->leasearr[i].leasetime != (u32_t)~0)) {
180 if (dhcps->leasearr[i].leasetime < curr_time) {
183 (void)memset_s(&(dhcps->leasearr[i]), sizeof(struct dyn_lease_addr), 0, sizeof(struct dyn_lease_addr));
184 dhcps->leasearr[i].flags = DHCPS_ADDRESS_FREE;
190 LWIP_STATIC void add_client_entry(struct dhcps *dhcps, unsigned int idx, struct dhcp_msg *client_msg)
194 if ((dhcps_option_given(dhcps, DHCP_OPTION_IDX_LEASE_TIME))
196 && (dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_LEASE_TIME) < LWIP_DHCPS_LEASE_TIME)
199 client_lease_time = (u32_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_LEASE_TIME);
202 (void)memset_s(&(dhcps->leasearr[idx]), sizeof(struct dyn_lease_addr), 0, sizeof(struct dyn_lease_addr));
203 if (memcpy_s(dhcps->leasearr[idx].cli_hwaddr, DHCP_CHADDR_LEN,
209 dhcps->leasearr[idx].leasetime = sys_now() + (LWIP_DHCPS_OFFER_TIME * 1000);
210 dhcps->leasearr[idx].cli_addr.addr = dhcps->start_addr.addr + idx;
211 dhcps->leasearr[idx].flags = DHCPS_ADDRESS_OFFERRED;
212 dhcps->leasearr[idx].proposed_leasetime = client_lease_time;
216 LWIP_STATIC int find_free_slot(struct dhcps *dhcps)
219 for (i = 0; i < dhcps->lease_num; i++) {
220 if ((dhcps->leasearr[i].flags == DHCPS_ADDRESS_FREE) &&
221 (htonl(dhcps->start_addr.addr + (u32_t)i) != ip_2_ip4(&dhcps->netif->ip_addr)->addr)) {
230 LWIP_STATIC struct dyn_lease_addr *find_client_lease(struct dhcps *dhcps, struct dhcp_msg *client_msg)
233 for (i = 0; i < dhcps->lease_num; i++) {
234 if (dhcps->leasearr[i].flags != DHCPS_ADDRESS_FREE) {
235 if (memcmp(dhcps->leasearr[i].cli_hwaddr, client_msg->chaddr, client_msg->hlen) == 0) {
238 return &(dhcps->leasearr[i]);
246 LWIP_STATIC ip4_addr_t validate_discover(struct dhcps *dhcps, struct dhcp_msg *client_msg,
257 if (dhcps_option_given(dhcps, DHCP_OPTION_IDX_REQUESTED_IP)) {
258 client_ip.addr = (u32_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_REQUESTED_IP);
264 if ((client_ip.addr >= dhcps->start_addr.addr) && (client_ip.addr <= dhcps->end_addr.addr)) {
265 idx = (int)(client_ip.addr - dhcps->start_addr.addr);
266 if ((dhcps->leasearr[idx].flags != DHCPS_ADDRESS_FREE) ||
267 (ntohl(client_ip.addr) == ip_2_ip4(&dhcps->netif->ip_addr)->addr)) {
279 idx = find_free_slot(dhcps);
286 client_ip.addr = dhcps->start_addr.addr + (u32_t)idx;
293 add_client_entry(dhcps, (unsigned int)idx, client_msg);
294 (*client_lease) = &(dhcps->leasearr[idx]);
302 if ((dhcps_option_given(dhcps, DHCP_OPTION_IDX_LEASE_TIME))
304 && (dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_LEASE_TIME) < LWIP_DHCPS_LEASE_TIME)
308 (*client_lease)->proposed_leasetime = (u32_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_LEASE_TIME);
335 LWIP_STATIC void handle_discover(struct netif *netif, struct dhcps *dhcps,
354 client_ip = validate_discover(dhcps, client_msg, &client_lease);
402 (void)udp_sendto_if_src(dhcps->pcb, out_msg, &dst_addr, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
408 (void)udp_sendto_if_src(dhcps->pcb, out_msg, IP_ADDR_BROADCAST, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
428 (void)udp_sendto_if_src(dhcps->pcb, out_msg, &client_ipaddr, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
440 (void)udp_sendto_if_src(dhcps->pcb, out_msg, IP_ADDR_BROADCAST, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
454 struct dhcps *dhcps = netif_get_dhcps(netif);
465 (!dhcps_option_given(dhcps, DHCP_OPTION_IDX_REQUESTED_IP))) {
479 requested_ip.addr = (u32_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_REQUESTED_IP);
485 if (dhcps_option_given(dhcps, DHCP_OPTION_IDX_REQUESTED_IP)) {
502 requested_ip.addr = (u32_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_REQUESTED_IP);
528 LWIP_STATIC void handle_request(struct netif *netif, struct dhcps *dhcps, struct dhcp_msg *client_msg,
619 (void)udp_sendto_if_src(dhcps->pcb, out_msg, &dst_addr, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
622 (void)udp_sendto_if_src(dhcps->pcb, out_msg, IP_ADDR_BROADCAST, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
644 (void)udp_sendto_if_src(dhcps->pcb, out_msg, &ip_send, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
659 LWIP_STATIC void handle_decline(struct netif *netif, struct dhcps *dhcps, struct dhcp_msg *client_msg,
664 if ((client_lease != NULL) && (dhcps_option_given(dhcps, DHCP_OPTION_IDX_REQUESTED_IP)) &&
666 if (client_lease->cli_addr.addr == (u32_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_REQUESTED_IP)) {
680 LWIP_STATIC void handle_inform(struct netif *netif, struct dhcps *dhcps, struct dhcp_msg *client_msg)
711 (void)udp_sendto_if_src(dhcps->pcb, out_msg, &dst_addr, DHCP_CLIENT_PORT, netif, &(netif->ip_addr));
719 LWIP_STATIC void handle_client_messages(struct netif *netif, struct dhcps *dhcps,
724 client_lease = find_client_lease(dhcps, client_msg);
727 handle_discover(netif, dhcps, client_msg, client_lease);
730 handle_request(netif, dhcps, client_msg, client_lease, serverid);
733 handle_decline(netif, dhcps, client_msg, client_lease);
745 handle_inform(netif, dhcps, client_msg);
753 err_t dhcps_parse_options(struct pbuf *p, struct dhcps *dhcps)
755 return dhcp_parse_reply(p, &dhcps->dhcp);
761 struct dhcps *dhcps = netif_get_dhcps(netif);
784 dhcps_clear_all_options(dhcps);
787 remove_stale_entries(dhcps);
814 if (dhcps_parse_options(p, dhcps) != ERR_OK) {
822 if (!dhcps_option_given(dhcps, DHCP_OPTION_IDX_MSG_TYPE)) {
829 msg_type = (u8_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_MSG_TYPE);
831 if (dhcps_option_given(dhcps, DHCP_OPTION_IDX_SERVER_ID)) {
835 serverid.addr = htonl((u32_t)dhcps_get_option_value(dhcps, DHCP_OPTION_IDX_SERVER_ID));
851 handle_client_messages(netif, dhcps, client_msg, serverid, msg_type);
859 struct dhcps *dhcps = NULL;
864 dhcps = netif_get_dhcps(netif);
875 if (dhcps != NULL) {
881 dhcps = (struct dhcps *)mem_malloc(sizeof(struct dhcps));
882 if (dhcps == NULL) {
889 (void)memset_s(dhcps, sizeof(struct dhcps), 0, sizeof(struct dhcps));
891 dhcps->pcb = udp_new();
892 if (dhcps->pcb == NULL) {
894 mem_free((void *)dhcps);
900 dhcps->pcb->ifindex = netif->ifindex;
904 dhcps->start_addr.addr = ntohl(ip_2_ip4(&netif->ip_addr)->addr & ip_2_ip4(&netif->netmask)->addr) + 1;
905 dhcps->end_addr.addr = ntohl(ip_2_ip4(&netif->ip_addr)->addr | (~ip_2_ip4(&netif->netmask)->addr)) - 1;
906 dhcps->lease_num = (u8_t)(dhcps->end_addr.addr - dhcps->start_addr.addr + 1);
907 if (dhcps->lease_num > LWIP_DHCPS_MAX_LEASE) {
908 dhcps->lease_num = LWIP_DHCPS_MAX_LEASE;
909 dhcps->end_addr.addr = dhcps->start_addr.addr + LWIP_DHCPS_MAX_LEASE - 1;
912 dhcps->start_addr.addr = ntohl(ipaddr_addr(start_ip));
913 dhcps->end_addr.addr = (u32_t)(dhcps->start_addr.addr +
916 ip4_addr_set_hton(&address_in_hton, &dhcps->start_addr);
921 udp_remove(dhcps->pcb);
922 mem_free((void *)dhcps);
926 ip4_addr_set_hton(&address_in_hton, &dhcps->end_addr);
930 dhcps->end_addr.addr = ntohl(ip_2_ip4(&netif->ip_addr)->addr | (~ip_2_ip4(&netif->netmask)->addr)) - 1;
932 dhcps->lease_num = (u8_t)(dhcps->end_addr.addr - dhcps->start_addr.addr + 1);
935 dhcps->netif = netif;
936 dhcps->pcb->so_options |= SOF_BROADCAST;
937 err = udp_bind(dhcps->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
939 udp_remove(dhcps->pcb);
940 mem_free((void *)dhcps);
944 err = udp_connect(dhcps->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
946 udp_remove(dhcps->pcb);
947 mem_free((void *)dhcps);
950 udp_recv(dhcps->pcb, dhcps_recv, netif);
954 ("dhcps_start(): DHCPS Conf:: netif addr = %"U32_F" dhcps start addr%"U32_F" dhcp end addr%"U32_F"\n",
955 ip_2_ip4(&netif->ip_addr)->addr, dhcps->start_addr.addr, dhcps->end_addr.addr));
960 netif_set_dhcps(netif, dhcps);
967 struct dhcps *dhcps = netif_get_dhcps(netif);
968 if (dhcps != NULL) {
970 if (dhcps->pcb != NULL) {
972 udp_remove(dhcps->pcb);
973 dhcps->pcb = NULL;
976 mem_free(dhcps);