⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp-client-conf.c

📁 this is sample about DHCP-agent
💻 C
📖 第 1 页 / 共 3 页
字号:
    }    return;}/* search through lower params and return the first match. */static client_conf_params_t *get_params_for(list_t *lower_params, ip_addr_t ip, eth_addr_t eth_addr){    client_conf_params_t *params = NULL;    list_rewind(lower_params);    while((params = list_next(lower_params)) != NULL) {        if((memcmp(&ip, &params->server_ip, sizeof(ip_addr_t)) == 0)            || (memcmp(&eth_addr, &params->server_hw_addr, sizeof(eth_addr_t)) == 0)) {            /* if a match is found break out. */            break;        } else {            /* check lower parameters too. */            if((params = get_params_for(params->lower_params, ip, eth_addr)) != NULL)                break;        }        /* otherwise we keep looping. */    }    return params;}/* utility routine to call on client_conf to find the best * param. this calls get_params_for on all lower parameters which is in of itself recursive. */static client_conf_params_t *find_best_params_for(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    client_conf_params_t *params;    params = get_params_for(cc->params->lower_params, ip_addr, eth_addr);    if(params == NULL)        params = cc->params;        return params;}/* given a list of strings identifying options we fill a bit array that references the option by the index. */static int client_conf_fill_option_bit_array_from_string_list(list_t *strings, uint8_t *option_bit_array){    char *option_string;    int i;    const char **option_strings = dhcp_option_conf_string_get_array();    list_rewind(strings);    while((option_string = list_next(strings)) != NULL) {        for(i = 0; i < MAX_OPTIONS_HANDLED; i++) {            if(!strcmp(option_strings[i], option_string)) {                option_bit_array[i] = 1;                break;            }        }        if(i > MAX_OPTIONS_HANDLED) {            ERROR_MESSAGE("specified unknown option: %s", option_string);            return 1;        }    }    return 0;}/* setup a variable in params_t. */static int client_conf_set_variable(client_conf_params_t *params, arg_symbol_t var_symbol, const char *var_value){    conf_var_t *var = NULL;    int *int_val;    uint16_t *uint16_val;    switch(var_symbol) {        /* string types can be created with no checking.         * just attempt a copy (will exit on failure). */    case CLIENT_VAR_HOSTNAME:    case CLIENT_VAR_SUBNET_MASK:        var = dhcp_conf_var_create(var_symbol, xstrdup(var_value));        break;        /* integer values greater than zero. use unsigned check         * to prevent negative values. */    case CLIENT_VAR_DHCP_DISCOVER_TIMEOUT:    case CLIENT_VAR_DHCP_REQUEST_TIMEOUT:    case CLIENT_VAR_ICMP_ECHO_TIMEOUT:    case CLIENT_VAR_ICMP_SUBNET_TIMEOUT:    case CLIENT_VAR_ARP_TIMEOUT:    case CLIENT_VAR_DHCP_DISCOVER_RETRIES:    case CLIENT_VAR_DHCP_REQUEST_RETRIES:    case CLIENT_VAR_ARP_RETRIES:    case CLIENT_VAR_ICMP_RETRIES:        if(!is_signed_numeric(var_value))  {            ERROR_MESSAGE("value not unsigned numeric as expected : %s\n", var_value);            return 1;        }        int_val = xmalloc(sizeof(int));        *int_val = atoi(var_value);        var = dhcp_conf_var_create(var_symbol, int_val);        break;    case CLIENT_VAR_RENEW_PERCENT:    case CLIENT_VAR_REBIND_PERCENT:    case CLIENT_VAR_INTERFACE_MTU:        uint16_val = xmalloc(sizeof(uint16_t));        sscanf(var_value, "%"SCNu16, uint16_val);        if(uint16_val != NULL)  {            var = dhcp_conf_var_create(var_symbol, uint16_val);            break;        }        /* fall through. */    default:        ERROR_MESSAGE("unable to understand assignment value of : %s\n", var_value);        return 1;    }    add_or_replace_var(params->variables, var);    return 0;}/* setup a boolean variable. */static int client_conf_set_variable_boolean(client_conf_params_t *params, arg_symbol_t var_symbol, uint8_t bool_val){    conf_var_t *var = NULL;    uint8_t *val;    val = xmalloc(sizeof(uint8_t));    *val = bool_val;    var = dhcp_conf_var_create(var_symbol, val);    add_or_replace_var(params->variables, var);    return 0;}static char *get_conf_options_fname(client_conf_t *cc){    char *fname;    stringbuffer_t *sb;    sb = stringbuffer_create();    stringbuffer_aprintf(sb, "%s/default.conf", DHCPSYSCONF_CLIENTDIR);    if(file_exists(stringbuffer_getstring(sb))) {        fname = xstrdup(stringbuffer_getstring(sb));        stringbuffer_destroy(sb);        return fname;    }    stringbuffer_clear(sb);    stringbuffer_aprintf(sb, "%s/%s.conf", DHCPSYSCONF_CLIENTDIR, cc->interface);    if(file_exists(stringbuffer_getstring(sb))) {        fname = xstrdup(stringbuffer_getstring(sb));        stringbuffer_destroy(sb);        return fname;    }    /* otherwise there is no configuration file and this is     * bad. signal a fatal error. */    FATAL_MESSAGE("configuration file missing -- looked for default.conf and <interface>.conf. exiting...");    exit(1); /* get rid of compiler warning. */}static uint8_t *get_conf_bit_array_var(client_conf_params_t *params, var_type_t var_type){    conf_var_t *var;    uint8_t *bit_array = NULL;    var = find_conf_var(var_type, params->variables);    if(var != NULL)        bit_array = dhcp_conf_var_get_val(var);    return bit_array;}static int get_conf_int_var(client_conf_params_t *params, var_type_t var_type, int default_val){    conf_var_t *var;    int *retries = NULL;    var = find_conf_var(var_type, params->variables);    if(var != NULL)        retries = dhcp_conf_var_get_val(var);    if(retries)        return *retries;    else        return default_val;}static uint8_t get_conf_bool_val(client_conf_params_t *params, var_type_t var_type, uint8_t default_val){    conf_var_t *var;    uint8_t *bool_val = NULL;    var = find_conf_var(var_type, params->variables);    if(var != NULL)        bool_val = dhcp_conf_var_get_val(var);    if(bool_val)        return *bool_val;    else        return default_val;}static uint16_t get_conf_uint16_val(client_conf_params_t *params, var_type_t var_type, uint16_t default_val){    conf_var_t *var;    uint16_t *uint16_val = NULL;    var = find_conf_var(var_type, params->variables);    if(var != NULL)        uint16_val = dhcp_conf_var_get_val(var);    if(uint16_val)        return *uint16_val;    else        return default_val;}static const char *get_conf_string_val(client_conf_params_t *params, var_type_t var_type, const char *default_val){    conf_var_t *var;    const char *string_val = NULL;    var = find_conf_var(var_type, params->variables);    if(var != NULL)        string_val = dhcp_conf_var_get_val(var);    if(string_val)        return string_val;    else        return default_val;}static int client_conf_load_options(client_conf_t *cc){    conf_t *conf;    list_t *directives;    conf = conf_create(commands, cc->conf_file);    if(conf_compile_directives(conf) == CONF_ERROR) {        ERROR_MESSAGE("error reading conf file: %s at line %d", cc->conf_file, conf_get_line_no(conf));        conf_destroy(conf);        return 1;    }    directives = conf_get_directives(conf);    /* read compiled directives. */    if(directive_group_handler(cc->params, conf->directives, DIRECTIVE_GROUP_NULL, NULL)) {        conf_destroy(conf);        return 1;    }    conf_destroy(conf);    return 0;}/* * * * * * * * * * * * * * * * * * * * params constructors/destructors   * * * * * * * * * * * * * * * * * * * */static client_conf_params_t *params_create(void){    client_conf_params_t *params;    params = xmalloc(sizeof(client_conf_params_t));    params->group_type = DIRECTIVE_GROUP_NULL;    params->variables = list_create();    params->lower_params = list_create();    params->append_options = list_create();    params->prepend_options = list_create();    params->override_options = list_create();    return params;}static void params_destroy_l(void *data){    client_conf_params_t *params = data;    list_destroy(params->variables, dhcp_conf_var_destroy_l);    list_destroy(params->lower_params, params_destroy_l);    dhcp_opt_destroy_option_list(params->append_options);    dhcp_opt_destroy_option_list(params->prepend_options);    dhcp_opt_destroy_option_list(params->override_options);    xfree(params);}static void params_destroy(client_conf_params_t *params){    /* call recursive function to destroy it and everything under it. */    params_destroy_l(params);    return;}/* * * * * * * * accessors * * * * * * * *//* get options to override. */list_t *client_conf_get_override_options(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    return cc->params->override_options;}/* get options to append. */list_t *client_conf_get_append_options(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    return cc->params->append_options;}/* get options to prepend. */list_t *client_conf_get_prepend_options(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    return cc->params->prepend_options;}/* get parameters to request: this is always from the top level. */uint8_t *client_conf_get_request_opt_bit_array(client_conf_t *cc){    return get_conf_bit_array_var(cc->params, CLIENT_VAR_REQUEST_OPTIONS);}/* get number of retries for dhcp: this is always from the top level. */int client_conf_get_dhcp_discovery_retries(client_conf_t *cc){    return get_conf_int_var(cc->params, CLIENT_VAR_DHCP_DISCOVER_RETRIES, CLIENT_DEFAULT_DHCP_DISCOVER_RETRIES);}/* get number of retries for dhcp: this is always from the top level. */int client_conf_get_dhcp_request_retries(client_conf_t *cc){    return get_conf_int_var(cc->params, CLIENT_VAR_DHCP_REQUEST_RETRIES, CLIENT_DEFAULT_DHCP_REQUEST_RETRIES);}/* get required options */uint8_t *client_conf_get_opt_required_bit_array(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    client_conf_params_t *params = find_best_params_for(cc, ip_addr, eth_addr);    return get_conf_bit_array_var(params, CLIENT_VAR_REQUIRE_OPTIONS);}/* get configure options */uint8_t *client_conf_get_opt_configure_bit_array(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    client_conf_params_t *params = find_best_params_for(cc, ip_addr, eth_addr);    return get_conf_bit_array_var(params, CLIENT_VAR_CONFIGURE_OPTIONS);}/* get number of times to attempt icmp operations where applicable */int client_conf_get_icmp_retries(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    client_conf_params_t *params = find_best_params_for(cc, ip_addr, eth_addr);    return get_conf_int_var(params, CLIENT_VAR_ICMP_RETRIES, CLIENT_DEFAULT_ICMP_RETRIES);}/* get number of times to attempt arp operations where applicable */int client_conf_get_arp_retries(client_conf_t *cc, ip_addr_t ip_addr, eth_addr_t eth_addr){    client_conf_params_t *params = find_best_params_for(cc, ip_addr, eth_addr);    return get_conf_int_var(params, CLIENT_VAR_ARP_RETRIES, CLIENT_DEFAULT_ARP_RETRIES);}/* get timeout threshold for dhcp discover operations. */int client_conf_get_dhcp_discover_timeout(client_conf_t *cc){    return get_conf_int_var(cc->params, CLIENT_VAR_DHCP_DISCOVER_TIMEOUT, CLIENT_DEFAULT_DHCP_DISCOVER_TIMEOUT);}/* get timeout threshold for dhcp request operations. */int client_conf_get_dhcp_request_timeout(client_conf_t *cc){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -