[dhcp-agent-commits] dhcp-agent/src dhcp-client-states.c,1.40,1.41 dhcp-client.c,1.30,1.31 dhcp-clie
Status: Alpha
Brought to you by:
actmodern
Update of /cvsroot/dhcp-agent/dhcp-agent/src In directory sc8-pr-cvs1:/tmp/cvs-serv17179/src Modified Files: dhcp-client-states.c dhcp-client.c dhcp-client.h dhcp-librawnet.h dhcp-libutil.h dhcp-packet-build.c dhcp-util.c Log Message: added dhcp-decline Index: dhcp-client-states.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client-states.c,v retrieving revision 1.40 retrieving revision 1.41 diff -C2 -d -r1.40 -r1.41 *** dhcp-client-states.c 25 Jun 2003 03:08:09 -0000 1.40 --- dhcp-client-states.c 26 Jun 2003 23:56:41 -0000 1.41 *************** *** 151,154 **** --- 151,176 ---- } + static list_t *client_build_decline_option_list(dhcp_client_control_t *dc, ip_addr_t ip_address) + { + list_t *options; + dhcp_opt_t *option; + + options = list_create(); + + /* client_id: RFC2131 says we MAY */ + option = dhcp_opt_create_from_internal_data(TAG_DHCP_CLIENT_ID, dc->client_id, (ETH_ADDR_LEN + 1)); + list_add(options, option); + + /* requested ip address: RFC2131 MUST. */ + option = dhcp_opt_create_from_internal_data(TAG_DHCP_REQUESTED_IP_ADDRESS, &ip_address, IP_ADDR_LEN); + list_add(options, option); + + /* dhcp message type. */ + option = dhcp_opt_create_message_type(DHCP_DECLINE_TM); + list_add(options, option); + + return options; + } + /* create DHCP RENEW option list. */ static list_t *client_build_renew_option_list(dhcp_client_control_t *dc) *************** *** 377,381 **** ð_addr)) { ERROR_MESSAGE("DHCP server assigned us a used address. Declining."); ! return STATE_DECLINE; } --- 399,411 ---- ð_addr)) { ERROR_MESSAGE("DHCP server assigned us a used address. Declining."); ! client_decline(dc); ! ! /* sleep for up to ten seconds: fixme: make this configurable. */ ! INFO_MESSAGE("sleeping before retry (for %"PRIu16" seconds)", 10); ! ! client_cache_delete_cache(dc->cache); ! sleep_random(10); ! ! return STATE_INIT; } *************** *** 750,755 **** client_cache_delete_tmp_cache(dc->cache); ! /* sleep for up to ten seconds: FIXME: make configurable. */ ! sleep((get_random_uint16()%11) + 1); INFO_MESSAGE("retrying."); --- 780,786 ---- client_cache_delete_tmp_cache(dc->cache); ! /* sleep for up to ten seconds: fixme: make this configurable. */ ! INFO_MESSAGE("sleeping before retry (for %"PRIu16" seconds)", 10); ! sleep_random(10); INFO_MESSAGE("retrying."); *************** *** 1010,1028 **** int client_release(dhcp_client_control_t *dc) { ! list_t *options; ! ip_addr_t my_addr; INFO_MESSAGE("sending DHCP RELEASE"); ! my_addr = rawnet_get_ip_addr(dc->rawnet); options = client_build_release_option_list(dc); ! build_dhcp_release(dc->rawnet, dc->xid, options, my_addr, dhcp_client_get_server_ip_address(dc), dhcp_client_get_server_hw_address(dc)); if(rawnet_send_packet(dc->rawnet) < 0) { ! ERROR_MESSAGE("could not send release."); return STATE_FATAL_ERROR; } --- 1041,1078 ---- int client_release(dhcp_client_control_t *dc) { ! list_t *cache_options, *options; ! ip_addr_t *my_addr = NULL; ! ip_addr_t ciaddr; ! dhcp_opt_t *option; INFO_MESSAGE("sending DHCP RELEASE"); ! cache_options = client_cache_load_options(dc->cache, 0); ! ! list_rewind(cache_options); ! while((option = list_next(cache_options)) != NULL) { ! ! if(DHCP_OPT_GET_TAG(option) == TAG_DHCP_REQUESTED_IP_ADDRESS) { ! my_addr = DHCP_OPT_TO_INTERNAL_DATA(option); ! break; ! } ! } ! ! if(my_addr == NULL) { /* if empty, don't release. */ ! dhcp_opt_destroy_option_list(cache_options); ! return STATE_FATAL_ERROR; ! } ! ! ciaddr = *my_addr; ! dhcp_opt_destroy_option_list(cache_options); options = client_build_release_option_list(dc); ! build_dhcp_release(dc->rawnet, dc->xid, options, ciaddr, dhcp_client_get_server_ip_address(dc), dhcp_client_get_server_hw_address(dc)); if(rawnet_send_packet(dc->rawnet) < 0) { ! ERROR_MESSAGE("could not send RELEASE."); return STATE_FATAL_ERROR; } *************** *** 1072,1074 **** --- 1122,1164 ---- exit(0); + } + + /* decline a lease. */ + int client_decline(dhcp_client_control_t *dc) + { + list_t *options, *decline_options; + dhcp_opt_t *option; + ip_addr_t *passed_ip = NULL; + + INFO_MESSAGE("performing DHCP decline"); + + options = client_cache_load_options(dc->cache, 0); + + list_rewind(options); + while((option = list_next(options)) != NULL) { + + if(DHCP_OPT_GET_TAG(option) == TAG_DHCP_REQUESTED_IP_ADDRESS) { + passed_ip = DHCP_OPT_TO_INTERNAL_DATA(option); + break; + } + } + + if(passed_ip == NULL) { + FATAL_MESSAGE("no address assigned and we're trying to decline. this is a bug, report me."); + } + + decline_options = client_build_decline_option_list(dc, *passed_ip); + dhcp_opt_destroy_option_list(options); + + build_dhcp_decline(dc->rawnet, dc->xid, dc->secs, decline_options); + if(rawnet_send_packet(dc->rawnet) < 0) { + ERROR_MESSAGE("could not send DECLINE."); + return STATE_FATAL_ERROR; + } + + /* a lot like release, our return here shouldn't matter since + * we are most likely called within a state. if not then the + * state simply shouldn't be handled. */ + + return STATE_FATAL_ERROR; } Index: dhcp-client.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client.c,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** dhcp-client.c 17 Jun 2003 07:34:16 -0000 1.30 --- dhcp-client.c 26 Jun 2003 23:56:42 -0000 1.31 *************** *** 87,90 **** --- 87,92 ---- client_inform, client_shutdown, + client_release, + client_decline, }; Index: dhcp-client.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client.h,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** dhcp-client.h 25 Jun 2003 03:08:32 -0000 1.19 --- dhcp-client.h 26 Jun 2003 23:56:42 -0000 1.20 *************** *** 95,98 **** --- 95,99 ---- extern int client_shutdown(dhcp_client_control_t *dc); extern int client_reinitialize(dhcp_client_control_t *dc); + extern int client_decline(dhcp_client_control_t *dc); /* client control. */ Index: dhcp-librawnet.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-librawnet.h,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** dhcp-librawnet.h 26 Jun 2003 13:55:09 -0000 1.18 --- dhcp-librawnet.h 26 Jun 2003 23:56:42 -0000 1.19 *************** *** 403,406 **** --- 403,407 ---- extern void build_dhcp_release(rawnet_t *net, uint32_t xid, list_t *options, ip_addr_t cip_addr, ip_addr_t sip_addr, eth_addr_t shw_addr); + extern void build_dhcp_decline(rawnet_t *net, uint32_t xid, time_t secs, list_t *options); /* arp packet routines. */ Index: dhcp-libutil.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-libutil.h,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** dhcp-libutil.h 25 May 2003 03:37:36 -0000 1.26 --- dhcp-libutil.h 26 Jun 2003 23:56:42 -0000 1.27 *************** *** 195,198 **** --- 195,199 ---- extern char *eth_addr_to_string(eth_addr_t eth_addr); extern char *ip_addr_to_string(ip_addr_t ip_addr); + extern void sleep_random(uint16_t max_sleep); /* type check routines */ Index: dhcp-packet-build.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-packet-build.c,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** dhcp-packet-build.c 25 Jun 2003 02:28:02 -0000 1.8 --- dhcp-packet-build.c 26 Jun 2003 23:56:42 -0000 1.9 *************** *** 429,432 **** --- 429,439 ---- } + void build_dhcp_decline(rawnet_t *net, uint32_t xid, time_t secs, list_t *options) + { + build_dhcp(net, xid, secs, 0, 0, 0, 0, ip_addr_broadcast, eth_broadcast, + 0, options, DHCP_BOOTP_REQUEST); + } + + /* * * * * * * * * * * ICMP routines. * Index: dhcp-util.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-util.c,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** dhcp-util.c 9 Jun 2003 02:03:12 -0000 1.15 --- dhcp-util.c 26 Jun 2003 23:56:42 -0000 1.16 *************** *** 582,583 **** --- 582,588 ---- } + + void sleep_random(uint16_t max_sleep) + { + sleep((get_random_uint16()%(max_sleep + 1)) + 1); + } |