[Dhcp-agent-commits] CVS: dhcp-agent dhcp-globconf.c,NONE,1.1 dhcp-globconf.h,NONE,1.1 dhcp-agent.h,
Status: Alpha
Brought to you by:
actmodern
From: Thamer Al-H. <act...@us...> - 2002-05-25 15:18:04
|
Update of /cvsroot/dhcp-agent/dhcp-agent In directory usw-pr-cvs1:/tmp/cvs-serv22957 Modified Files: dhcp-agent.h dhcp-client-conf.c dhcp-client-control.c dhcp-icmp-discovery.c Added Files: dhcp-globconf.c dhcp-globconf.h Log Message: linked in globconfig -- now uses two variables --- NEW FILE: dhcp-globconf.c --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/dhcp-globconf.c,v 1.1 2002/05/25 15:18:01 actmodern Exp $ * * Copyright 2001 Thamer Alharbash <tm...@wh...> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Global config options -- this breaks our usual order of doing things. * Usually we pass the datum instead of keeping a local copy and modifying it. * However, in the case of global defaults they are by definition global and should * only be accessed from one datum. Thus we keep a local copy we initialize. * */ #include <dhcp-agent.h> #include <dhcp-globconf.h> /* change this before... */ #define GLOBAL_OPTIONS_LEN 2 /* ... changing size of this */ /* array holding information on converting global config options and their defaults. * do not change order unless you change defines in dhcp-globconf.h! */ static glob_conf_t global_config_options[] = { { "client-dhcp-retries", string_to_int32, int32_to_string, sizeof(int32_t), DHCP_CLIENT_RETRIES_S, NULL }, { "client-icmp-retries", string_to_int32, int32_to_string, sizeof(int32_t), DHCP_CLIENT_RETRIES_S, NULL }, }; /* useful for dumping defaults. */ static int glob_conf_dump(const char *filename) { int i; FILE *fp; char *var_string; fp = file_open_or_create_safe(filename, "w"); if(fp == NULL) return -1; for(i = 0;i < GLOBAL_OPTIONS_LEN; i++) { var_string = global_config_options[i].convert_option_to_string(global_config_options[i].val, global_config_options[i].size); if(var_string == NULL) fatal_error("could not convert option to string -- this is a bug report it."); fprintf(fp,"%s=%s\n", global_config_options[i].name, var_string); xfree(var_string); } fclose(fp); return 0; } static void glob_conf_set_defaults(void) { int i; for(i = 0;i < GLOBAL_OPTIONS_LEN;i++) { if(global_config_options[i].val == NULL) { global_config_options[i].val = global_config_options[i].convert_option_to_internal(global_config_options[i].default_val); if(global_config_options[i].val == NULL) fatal_error("glob-conf: unable to convert value for option %s -- default! this is a bug. report it!"); } } return; } /* read configuration options in and set variables. */ int init_glob_conf(unsigned char *filename) { FILE *fp; int i; /* we're ok. all config options are in directories only * writable by us. */ if(!file_exists(filename)) { /* dump defaults if not available. */ glob_conf_set_defaults(); glob_conf_dump(filename); return 0; } fp = file_open_or_create_safe(filename, "r"); if(fp == NULL) fatal_error("glob-conf: could not open configuration file: %s: %s", filename, strerror(errno)); while(!file_get_var_string(fp)) { if(file_parse_string(PARSE_DOUBLE_STRINGS)) { fclose(fp); /* fixme: add parse error. */ return -1; } for(i = 0;i < GLOBAL_OPTIONS_LEN;i++) { if(string_matches(global_config_options[i].name, file_get_var_name())) { /* we have a match. */ global_config_options[i].val = global_config_options[i].convert_option_to_internal(file_get_var_val()); if(global_config_options[i].val == NULL) fatal_error("glob-conf: unable to convert value for option %s -- munged variable?"); break; } } } fclose(fp); /* one more pass to convert defaults if needed. * a user could always truncate the file we dumped. */ glob_conf_set_defaults(); return 0; } void *glob_conf_get_val(int val_index) { return global_config_options[val_index].val; } --- NEW FILE: dhcp-globconf.h --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/dhcp-globconf.h,v 1.1 2002/05/25 15:18:01 actmodern Exp $ * * Copyright 2001 Thamer Alharbash <tm...@wh...> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Global configuration options. Accessible from anywhere through * glob-conf code. */ #ifndef GLOBAL_CONF_H #define GLOBAL_CONF_H typedef struct { const char *name; void *(*convert_option_to_internal)(const char *); char *(*convert_option_to_string)(const unsigned char *, int); size_t size; const char *default_val; void *val; } glob_conf_t; extern int init_glob_conf(unsigned char *filename); extern void *glob_conf_get_val(int val_index); /* indexes to values. */ #define CLIENT_DHCP_RETRIES 0 #define CLIENT_ICMP_RETRIES 1 /* default string values. */ #define DHCP_CLIENT_RETRIES_S "3" /* governs all retries. */ #endif /* GLOBAL_CONF_H */ Index: dhcp-agent.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-agent.h,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** dhcp-agent.h 25 May 2002 14:02:30 -0000 1.38 --- dhcp-agent.h 25 May 2002 15:18:00 -0000 1.39 *************** *** 165,169 **** #define DISCOVER_OFFER_RETRIES 3 - #define DHCP_CLIENT_RETRIES_S "3" /* * * * * * * * * * * --- 165,168 ---- *************** *** 346,352 **** unsigned char options[MAX_OPTIONS_HANDLED]; /* which options should we handle. */ - int discover_offer_retries; /* amount of times we're willing to keep - * rediscovering before giving up. */ - } client_conf_t; --- 345,348 ---- *************** *** 368,372 **** unsigned char *class_id, *client_id; /* client_id, class_id */ int state; /* our current state. */ ! int discover_offer_retries; /* how many times we should discover/offer before giving up. */ } dhcp_client_control_t; --- 364,368 ---- unsigned char *class_id, *client_id; /* client_id, class_id */ int state; /* our current state. */ ! int discover_offer_retries; /* counter for retries on discover_offer */ } dhcp_client_control_t; Index: dhcp-client-conf.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-client-conf.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** dhcp-client-conf.c 11 Feb 2002 18:02:24 -0000 1.5 --- dhcp-client-conf.c 25 May 2002 15:18:01 -0000 1.6 *************** *** 53,57 **** client_conf_reset_options(cc); cc->interface = dc->interface; - cc->discover_offer_retries = DISCOVER_OFFER_RETRIES; return cc; } --- 53,56 ---- Index: dhcp-client-control.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-client-control.c,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** dhcp-client-control.c 19 May 2002 00:34:22 -0000 1.16 --- dhcp-client-control.c 25 May 2002 15:18:01 -0000 1.17 *************** *** 23,26 **** --- 23,27 ---- #include <dhcp-agent.h> + #include <dhcp-globconf.h> /* Utility routines to update counters and time stamps. */ *************** *** 49,54 **** int dhcp_client_discover_offer_can_retry(dhcp_client_control_t *dc) { dc->discover_offer_retries++; ! if(dc->discover_offer_retries > dc->conf->discover_offer_retries) return 1; --- 50,57 ---- int dhcp_client_discover_offer_can_retry(dhcp_client_control_t *dc) { + uint32_t *discover_offer_retries = glob_conf_get_val(CLIENT_DHCP_RETRIES); dc->discover_offer_retries++; ! ! if(dc->discover_offer_retries > *(discover_offer_retries)) return 1; Index: dhcp-icmp-discovery.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/dhcp-icmp-discovery.c,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** dhcp-icmp-discovery.c 19 May 2002 23:07:53 -0000 1.7 --- dhcp-icmp-discovery.c 25 May 2002 15:18:01 -0000 1.8 *************** *** 28,31 **** --- 28,32 ---- #include <dhcp-agent.h> + #include <dhcp-globconf.h> /* Check for icmp mask response. */ *************** *** 142,146 **** /* FIXME:x get rid of this magic number -- global config options? */ ! int sends = 3; /* Our algorithm works as such: send out an ICMP echo request --- 143,147 ---- /* FIXME:x get rid of this magic number -- global config options? */ ! int *sends = glob_conf_get_val(CLIENT_ICMP_RETRIES); /* Our algorithm works as such: send out an ICMP echo request *************** *** 171,179 **** /* allocate latency array. */ ! latency = xmalloc(sizeof(int) * sends); send_count = 0; /* fill up our latency array. */ ! for(send_count = 0; send_count < sends; send_count++) latency[send_count] = icmp_do_echo(net, *(host_addr), dest_mac); --- 172,180 ---- /* allocate latency array. */ ! latency = xmalloc(sizeof(int) * *(sends)); send_count = 0; /* fill up our latency array. */ ! for(send_count = 0; send_count < *(sends); send_count++) latency[send_count] = icmp_do_echo(net, *(host_addr), dest_mac); *************** *** 189,193 **** /* check our latency if it's all -1 */ unreachable_count = 0; ! for(send_count = 0; send_count < sends; send_count++) { if(latency[send_count] == -1) unreachable_count++; --- 190,194 ---- /* check our latency if it's all -1 */ unreachable_count = 0; ! for(send_count = 0; send_count < *(sends); send_count++) { if(latency[send_count] == -1) unreachable_count++; *************** *** 209,213 **** /* get highest. */ highest_latency = 0; ! for(send_count = 0; send_count < sends; send_count++) { if(latency[send_count] > highest_latency) highest_latency = latency[send_count]; --- 210,214 ---- /* get highest. */ highest_latency = 0; ! for(send_count = 0; send_count < *(sends); send_count++) { if(latency[send_count] > highest_latency) highest_latency = latency[send_count]; *************** *** 215,219 **** /* replace unreachables with highest multiplied by two */ ! for(send_count = 0; send_count < sends; send_count++) { if(latency[send_count] == -1) latency[send_count] = highest_latency * 2; --- 216,220 ---- /* replace unreachables with highest multiplied by two */ ! for(send_count = 0; send_count < *(sends); send_count++) { if(latency[send_count] == -1) latency[send_count] = highest_latency * 2; *************** *** 224,232 **** *average_latency = 0; ! for(send_count = 0; send_count < sends; send_count++) { *average_latency += latency[send_count]; } ! *average_latency /= sends; } --- 225,233 ---- *average_latency = 0; ! for(send_count = 0; send_count < *(sends); send_count++) { *average_latency += latency[send_count]; } ! *average_latency /= *(sends); } |