[dhcp-agent-commits] dhcp-agent/src dhcp-client-conf.c,1.5,1.6 dhcp-client-states.c,1.5,1.6 dhcp-cli
Status: Alpha
Brought to you by:
actmodern
Update of /cvsroot/dhcp-agent/dhcp-agent/src In directory sc8-pr-cvs1:/tmp/cvs-serv24303/src Modified Files: dhcp-client-conf.c dhcp-client-states.c dhcp-client.c dhcp-client.h dhcp-convert.c dhcp-libutil.h dhcp-timer.c Log Message: rebind/renew implemented Index: dhcp-client-conf.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client-conf.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-client-conf.c 22 Nov 2002 15:30:37 -0000 1.5 --- dhcp-client-conf.c 24 Nov 2002 01:10:00 -0000 1.6 *************** *** 37,42 **** #include "dhcp-options-strings.h" - #define DEFAULT_CONFIG_OPTIONS_LEN 8 - /* Basic network configuration. * Here we keep the options we want by default. */ --- 37,40 ---- *************** *** 97,101 **** return -1; ! for(i = 0; i < DEFAULT_CONFIG_OPTIONS_LEN; i++) { if(fprintf(fp, "\"%s\"\n", dhcp_option_string_get(i)) < 0) { --- 95,99 ---- return -1; ! for(i = 0; i < NELMS(default_config_options); i++) { if(fprintf(fp, "\"%s\"\n", dhcp_option_string_get(i)) < 0) { *************** *** 114,118 **** int i; ! for(i = 0; i < DEFAULT_CONFIG_OPTIONS_LEN; i++) { cc->options[default_config_options[i]] = 1; } --- 112,116 ---- int i; ! for(i = 0; i < NELMS(default_config_options); i++) { cc->options[default_config_options[i]] = 1; } Index: dhcp-client-states.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client-states.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-client-states.c 19 Nov 2002 19:44:44 -0000 1.5 --- dhcp-client-states.c 24 Nov 2002 01:10:00 -0000 1.6 *************** *** 44,48 **** ***************************/ ! /* Create DHCP RELEASE option list. */ static list_t *client_build_release_option_list(dhcp_client_control_t *dc) { --- 44,48 ---- ***************************/ ! /* create DHCP RELEASE option list. */ static list_t *client_build_release_option_list(dhcp_client_control_t *dc) { *************** *** 62,74 **** } ! static dhcp_option_t *client_build_parameter_request_list(dhcp_client_control_t *dc) ! { ! dhcp_option_t *option; ! ! option = dhcp_build_parameter_request_list_option(client_conf_get_opt_bit_array(dc->conf)); ! return option; ! } ! ! /* This is used for both requests and discovery. */ static list_t *client_build_option_list(dhcp_client_control_t *dc) { --- 62,66 ---- } ! /* create DISCOVER and REQUEST option list. */ static list_t *client_build_option_list(dhcp_client_control_t *dc) { *************** *** 101,105 **** } ! /* Create DHCP DISCOVER option list. */ static list_t *client_build_discover_option_list(dhcp_client_control_t *dc) { --- 93,97 ---- } ! /* create DHCP DISCOVER option list. */ static list_t *client_build_discover_option_list(dhcp_client_control_t *dc) { *************** *** 108,112 **** dhcp_option_t *option; ! option = client_build_parameter_request_list(dc); options = add_to_list(options, option); --- 100,104 ---- dhcp_option_t *option; ! option = dhcp_build_parameter_request_list_option(client_conf_get_opt_bit_array(dc->conf)); options = add_to_list(options, option); *************** *** 121,125 **** } ! /* Create DHCP REQUEST option list. */ static list_t *client_build_request_option_list(dhcp_client_control_t *dc) { --- 113,117 ---- } ! /* create DHCP REQUEST option list. */ static list_t *client_build_request_option_list(dhcp_client_control_t *dc) { *************** *** 138,142 **** } ! /* Create DHCP REBIND option list. */ static list_t *client_build_renew_option_list(dhcp_client_control_t *dc) { --- 130,134 ---- } ! /* create DHCP RENEW option list. */ static list_t *client_build_renew_option_list(dhcp_client_control_t *dc) { *************** *** 155,164 **** } /****************** * Check Routines * ******************/ ! /* Check the requested options. ! * We use the globconf option. */ static int client_check_requested_options(dhcp_client_control_t *dc) --- 147,163 ---- } + /* create DHCP REBIND option list. */ + static list_t *client_build_rebind_option_list(dhcp_client_control_t *dc) + { + /* just call renew_option_list since it's the same. */ + return(client_build_renew_option_list(dc)); + } + /****************** * Check Routines * ******************/ ! /* check the requested options. ! * we use the globconf option. */ static int client_check_requested_options(dhcp_client_control_t *dc) *************** *** 182,186 **** } ! /* Basic DHCP test on incoming rawnet packet. */ static int client_check_dhcp(dhcp_client_control_t *dc) { --- 181,185 ---- } ! /* basic DHCP test on incoming rawnet packet. */ static int client_check_dhcp(dhcp_client_control_t *dc) { *************** *** 228,231 **** --- 227,231 ---- } + /* check for valid renew response. */ static int client_check_renew(void *arg) { *************** *** 235,240 **** } /************* ! * Utilities.* *************/ --- 235,248 ---- } + /* check for valid renew response. */ + static int client_check_rebind(void *arg) + { + /* we're expecting the same as request :-) */ + return (client_check_request(arg)); + + } + /************* ! * Utilities * *************/ *************** *** 267,272 **** } /************* ! * ARP/UNARP.* *************/ --- 275,304 ---- } + time_t compensate_timeout_with_timer(dhcp_client_control_t *dc) + { + uint32_t next_timer; + time_t timeout; + + /* we realize time_t may not be uint32_t, however we're only + * going to set the max timeout if it is lower so this is + * safe. we shouldn't overflow. */ + + next_timer = timer_peek_next_timer(dc->timer); + if(next_timer < RECOMMENDED_MAX_SECS_WAIT) { + + if(next_timer != 0) + timeout = next_timer/2; + else + timeout = next_timer; + + } else { + timeout = RECOMMENDED_MAX_SECS_WAIT; + } + + return timeout; + } + /************* ! * ARP/UNARP * *************/ *************** *** 323,326 **** --- 355,360 ---- int retval; + INFO_MESSAGE("attempting DHCP DISCOVER"); + options = client_build_discover_option_list(dc); *************** *** 385,389 **** } ! /* do client_request_ack transaction. */ int client_request_ack(dhcp_client_control_t *dc) { --- 419,423 ---- } ! /* do request/ack transaction. */ int client_request_ack(dhcp_client_control_t *dc) { *************** *** 391,394 **** --- 425,430 ---- int retval; + INFO_MESSAGE("attempting DHCP REQUEST"); + /* Build options along with the options in our cache. */ *************** *** 426,431 **** default: ! FATAL_MESSAGE ! ("dhcp-client-states: invalid return value from raw network handler -- this a bug report it."); } --- 462,466 ---- default: ! FATAL_MESSAGE("invalid return value from raw network handler -- this a bug report it."); } *************** *** 446,454 **** } ! /* release lease. */ int client_release(dhcp_client_control_t *dc) { list_t *options; options = client_build_release_option_list(dc); --- 481,491 ---- } ! /* do release */ int client_release(dhcp_client_control_t *dc) { list_t *options; + INFO_MESSAGE("sending DHCP RELEASE"); + options = client_build_release_option_list(dc); *************** *** 478,486 **** } ! /* do setup (call do_sysconf) */ int client_setup(dhcp_client_control_t *dc) { list_t *options; options = client_cache_load_option_string_list(dc->cache, 0); if(options == NULL) { --- 515,525 ---- } ! /* do system setup */ int client_setup(dhcp_client_control_t *dc) { list_t *options; + INFO_MESSAGE("performing system configuration"); + options = client_cache_load_option_string_list(dc->cache, 0); if(options == NULL) { *************** *** 506,512 **** } ! /* wait for timers and other interrupts . */ int client_wait(dhcp_client_control_t *dc) { /* take our raw network connection down. */ rawnet_down(dc->rawnet); --- 545,553 ---- } ! /* wait for timers and other interrupts */ int client_wait(dhcp_client_control_t *dc) { + INFO_MESSAGE("entering wait stage"); + /* take our raw network connection down. */ rawnet_down(dc->rawnet); *************** *** 567,586 **** } ! /* do a rebind. */ int client_renew(dhcp_client_control_t *dc) { list_t *options; int retval; - options = client_build_renew_option_list(dc); build_dhcp_request_unicast(dc->rawnet, dc->xid, dc->secs, options, dc->sip_addr, dc->shw_addr); retval = ! rawnet_packet_transact(dc->rawnet, dc, NULL, client_check_renew, RECOMMENDED_MAX_SECS_WAIT); switch (retval) { case RAWNET_TIMEOUT: ! ERROR_MESSAGE("timeout on DHCP request."); ! return STATE_FATAL_ERROR; case RAWNET_OK: --- 608,634 ---- } ! /* do a renew: this is exactly like rebind only we're unicasting. */ int client_renew(dhcp_client_control_t *dc) { list_t *options; int retval; + + INFO_MESSAGE("attempting DHCP RENEW"); + + options = client_build_renew_option_list(dc); build_dhcp_request_unicast(dc->rawnet, dc->xid, dc->secs, options, dc->sip_addr, dc->shw_addr); + /* find out how much time we have until the next timer. + * if we have no other timers it's an error, however + * it will be caught later now just do our best. */ + retval = ! rawnet_packet_transact(dc->rawnet, dc, NULL, client_check_renew, compensate_timeout_with_timer(dc)); ! switch (retval) { case RAWNET_TIMEOUT: ! return STATE_WAIT; /* no response: we've timed out. */ case RAWNET_OK: *************** *** 604,607 **** --- 652,701 ---- * here we'll fall into rebind time at the next timer * trigger. */ + + return STATE_WAIT; + } + + + /* do a rebind. */ + int client_rebind(dhcp_client_control_t *dc) + { + list_t *options; + int retval; + + INFO_MESSAGE("Attempting DHCP REBIND"); + + options = client_build_rebind_option_list(dc); + build_dhcp_request_broadcast(dc->rawnet, dc->xid, dc->secs, options); + + retval = + rawnet_packet_transact(dc->rawnet, dc, NULL, client_check_rebind, compensate_timeout_with_timer(dc)); + + switch (retval) { + + case RAWNET_TIMEOUT: + return STATE_WAIT; /* no response. we've timed out. */ + + case RAWNET_OK: + break; + + case RAWNET_ERROR: + ERROR_MESSAGE("received error from raw network handler."); + return STATE_FATAL_ERROR; + + default: + FATAL_MESSAGE + ("invalid return value from raw network handler -- this a bug report it."); + } + + /* if we got a pack we need to setup our timers. */ + if(dhcp_is_type(dc->rawnet->dhcp_p, DHCP_DHCPACK_TM)) { + + client_setup_timers(dc); + + /* since we did a rebind our server's ip address and mac address may be different. */ + dhcp_client_set_server_ip_address(dc, ip_get_source_addr(dc->rawnet->ip_p)); + dhcp_client_set_server_hw_address(dc, eth_get_src_address(dc->rawnet->ether_p)); + + } return STATE_WAIT; Index: dhcp-client.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client.c,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** dhcp-client.c 19 Nov 2002 19:44:44 -0000 1.6 --- dhcp-client.c 24 Nov 2002 01:10:00 -0000 1.7 *************** *** 84,87 **** --- 84,88 ---- client_release, client_renew, + client_rebind, client_fatal_error, client_do_shutdown, *************** *** 251,255 **** case TIMER_REBIND: ! state = STATE_REQUEST_ACK; break; --- 252,256 ---- case TIMER_REBIND: ! state = STATE_REBIND; break; Index: dhcp-client.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-client.h 19 Nov 2002 19:44:44 -0000 1.5 --- dhcp-client.h 24 Nov 2002 01:10:00 -0000 1.6 *************** *** 70,75 **** #define STATE_RELEASE 4 #define STATE_RENEW 5 ! #define STATE_FATAL_ERROR 6 ! #define STATE_DO_SHUTDOWN 7 --- 70,76 ---- #define STATE_RELEASE 4 #define STATE_RENEW 5 ! #define STATE_REBIND 6 ! #define STATE_FATAL_ERROR 7 ! #define STATE_DO_SHUTDOWN 8 *************** *** 86,89 **** --- 87,91 ---- extern int client_release(dhcp_client_control_t *dc); extern int client_renew(dhcp_client_control_t *dc); + extern int client_rebind(dhcp_client_control_t *dc); extern int client_setup(dhcp_client_control_t *dc); extern int client_fatal_error(dhcp_client_control_t *dc); Index: dhcp-convert.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-convert.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** dhcp-convert.c 16 Nov 2002 00:23:43 -0000 1.3 --- dhcp-convert.c 24 Nov 2002 01:10:00 -0000 1.4 *************** *** 565,569 **** char *network_int_to_string(const unsigned char *data, int data_len, size_t size, int si) { ! char *string; if(size > data_len) --- 565,569 ---- char *network_int_to_string(const unsigned char *data, int data_len, size_t size, int si) { ! char *string = NULL; if(size > data_len) *************** *** 739,743 **** uint16_t *val; int len = 0; ! char *ptr; const char *cp; --- 739,743 ---- uint16_t *val; int len = 0; ! char *ptr = NULL; const char *cp; Index: dhcp-libutil.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-libutil.h,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** dhcp-libutil.h 19 Nov 2002 19:44:44 -0000 1.7 --- dhcp-libutil.h 24 Nov 2002 01:10:00 -0000 1.8 *************** *** 59,62 **** --- 59,63 ---- /* trigger object. */ typedef struct { + uint32_t time_set; uint32_t seconds; uint32_t id; *************** *** 194,197 **** --- 195,199 ---- extern uint32_t timer_get_current_id(timer_keeper_t *timer); extern void reinitialize_timer(timer_keeper_t *timer); + extern uint32_t timer_peek_next_timer(timer_keeper_t *timer); /* var file. */ *************** *** 246,249 **** --- 248,255 ---- #define MILLISECOND_RAND_TIMEOUT 1000 #define RECOMMENDED_MAX_SECS_WAIT 64 + + /* constant time values. */ + + #define INFINITE_TIME 0xFFFFFFFF /* macros. */ Index: dhcp-timer.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-timer.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** dhcp-timer.c 19 Nov 2002 19:44:44 -0000 1.4 --- dhcp-timer.c 24 Nov 2002 01:10:00 -0000 1.5 *************** *** 58,61 **** --- 58,62 ---- trigger->seconds = trigger_time; trigger->id = id; + trigger->time_set = time(NULL); return trigger; *************** *** 105,122 **** timer_trigger_t *trigger = create_trigger(trigger_time, id); timer->triggers = add_to_list(timer->triggers, trigger); return; } ! /* timer setup next trigger. */ ! int timer_set_next(timer_keeper_t *timer) { timer_trigger_t *trigger; if(timer->triggers == NULL) ! return 1; - timer->triggers = sort_list(timer->triggers, compare_triggers_shortest); trigger = timer->triggers->data; set_alarm(trigger->seconds); --- 106,152 ---- timer_trigger_t *trigger = create_trigger(trigger_time, id); timer->triggers = add_to_list(timer->triggers, trigger); + timer->triggers = sort_list(timer->triggers, compare_triggers_shortest); return; } ! /* utility to grab next valid timer: this will delete ones which have passed since. */ ! static timer_trigger_t *get_next_timer(timer_keeper_t *timer) { + uint32_t time_left; timer_trigger_t *trigger; + uint32_t time_now; + + begin: if(timer->triggers == NULL) ! return NULL; trigger = timer->triggers->data; + time_now = time(NULL); + time_left = trigger->seconds - (time_now - trigger->time_set); + + if(time_left > trigger->seconds) { + /* overflow. get the next timer. */ + timer->triggers = remove_from_list(timer->triggers, timer->triggers->data); + destroy_trigger(trigger); + + goto begin; + } + + trigger->seconds = time_left; + trigger->time_set = time_now; + return trigger; + } + + /* timer setup next trigger. */ + int timer_set_next(timer_keeper_t *timer) + { + + timer_trigger_t *trigger; + trigger = get_next_timer(timer); + + if(trigger == NULL) + return 1; set_alarm(trigger->seconds); *************** *** 133,134 **** --- 163,177 ---- return timer->current_id; } + + uint32_t timer_peek_next_timer(timer_keeper_t *timer) + { + timer_trigger_t *trigger; + + trigger = get_next_timer(timer); + + if(trigger == NULL) + return INFINITE_TIME; + + return trigger->seconds; + } + |