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

📄 dhcp-client-conf.c

📁 this is sample about DHCP-agent
💻 C
📖 第 1 页 / 共 3 页
字号:
    return get_conf_int_var(cc->params, CLIENT_VAR_DHCP_REQUEST_TIMEOUT, CLIENT_DEFAULT_DHCP_REQUEST_TIMEOUT);}/* get timeout threshold for arp operations. */int client_conf_get_arp_timeout(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_TIMEOUT, CLIENT_DEFAULT_ARP_TIMEOUT);}/* get timeout threshold for icmp echo operations. */int client_conf_get_icmp_echo_timeout(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_ECHO_TIMEOUT, CLIENT_DEFAULT_ICMP_ECHO_TIMEOUT);}/* get timeout threshold for icmp subnet operations. */int client_conf_get_icmp_subnet_timeout(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_SUBNET_TIMEOUT, CLIENT_DEFAULT_ICMP_SUBNET_TIMEOUT);}/* get the hostname option the client should set: always from the top level */const char *client_conf_get_hostname(client_conf_t *cc){    return get_conf_string_val(cc->params, CLIENT_VAR_HOSTNAME, NULL);}/* find out whether or not we should attempt to measure router latency. */uint8_t client_conf_get_do_measure_router_latency_icmp(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_bool_val(params, CLIENT_VAR_DO_MEASURE_ROUTER_LATENCY_ICMP, CLIENT_DEFAULT_DO_MEASURE_LATENCY_ICMP);}/* get mtu setting. */int client_conf_get_default_mtu(client_conf_t *cc){    return get_conf_uint16_val(cc->params, CLIENT_VAR_INTERFACE_MTU, CLIENT_DEFAULT_MTU);}const char *client_conf_get_default_subnet_mask(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_string_val(params, CLIENT_VAR_SUBNET_MASK, CLIENT_DEFAULT_SUBNET_MASK);}int client_conf_get_renew_percent(client_conf_t *cc){    return get_conf_uint16_val(cc->params, CLIENT_VAR_RENEW_PERCENT, CLIENT_DEFAULT_RENEW_PERCENT);}int client_conf_get_rebind_percent(client_conf_t *cc){    return get_conf_uint16_val(cc->params, CLIENT_VAR_REBIND_PERCENT, CLIENT_DEFAULT_REBIND_PERCENT);}/* * * * * * * * * * * * * directive handlers. * * * * * * * * * * * * */static int directive_group_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    list_t *directive_list;    directive_t *directive;    /* setup group before handling directives under it. */    params->group_type = group_type;    /* copy over data if applicable. */    switch(group_type) {    case DIRECTIVE_GROUP_IP_ADDRESS:        memcpy(&params->server_ip, group_data, IP_ADDR_LEN);        break;    case DIRECTIVE_GROUP_MAC_ADDRESS:        memcpy(&params->server_hw_addr, group_data, ETH_ADDR_LEN);        break;    case DIRECTIVE_GROUP_NULL:  /* fall through. */    default:        break;    }    /* now process group. */    directive_list = directive_data;    list_rewind(directive_list);    while((directive = list_next(directive_list)) != NULL) {        if(directive_handlers[directive->command_code](params, directive, group_type, group_data))            return 1;    }    return 0;}static int directive_set_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    directive_t *directive = directive_data;    list_t *args;    arg_symbol_t *var_symbol;    char *var_value;    args = directive->arguments;    var_symbol = list_get_by_index(args, 0);    var_value = list_get_by_index(args, 2);    if(client_conf_set_variable(params, *var_symbol, var_value)) {        ERROR_MESSAGE("configuration: error unable to set variable %s to %s", var_strings[*var_symbol], var_value);        return 1;    }    return 0;}/* procified routine for request/require/configure etc: these all deal with bit arrays. */static int directive_option_array_handler_proc(client_conf_params_t *params, void *directive_data,                                                directive_group_t group_type, void *group_data,                                                var_type_t type){    directive_t *directive = directive_data;    list_t *args;    list_t *string_list;    uint8_t *options;    /* the first argument is the string list we want. */    args = directive->arguments;    string_list = list_first(args);    /* create bit array. */    options = xcalloc(MAX_OPTIONS_HANDLED_NUM * sizeof(uint8_t));    if(client_conf_fill_option_bit_array_from_string_list(string_list, options)) {        xfree(options);        return 1;    }    add_or_overwrite_bit_array_var(params->variables, options, type);    return 0;}static int directive_request_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    return directive_option_array_handler_proc(params, directive_data,                                                group_type, group_data,                                                CLIENT_VAR_REQUEST_OPTIONS);}static int directive_require_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    return directive_option_array_handler_proc(params, directive_data,                                                group_type, group_data,                                                CLIENT_VAR_REQUIRE_OPTIONS);}static int directive_configure_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    return directive_option_array_handler_proc(params, directive_data,                                                group_type, group_data,                                                CLIENT_VAR_CONFIGURE_OPTIONS);}static int directive_user_defined_override(client_conf_params_t *params, const char *option_name, dhcp_opt_t *option){    if(option == NULL) {        ERROR_MESSAGE("unknown option %s to override", option_name);        return 1;    }    list_add(params->override_options, option);    return 0;}static int directive_user_defined_append(client_conf_params_t *params, const char *option_name, dhcp_opt_t *option){    if(option == NULL) {        ERROR_MESSAGE("unknown option %s to append", option_name);        return 1;    }    /* cannot append unless it's a list type. */    if(dhcp_opt_get_type(option) != DHCP_OPT_LIST)  {        ERROR_MESSAGE("user config attempted to append to: %s", option_name);        ERROR_MESSAGE("cannot append option which is not a list type");        ERROR_MESSAGE("you can only override options which are not lists.");        dhcp_opt_destroy(option);        return 1;    }    list_add(params->append_options, option);    return 0;}static int directive_user_defined_prepend(client_conf_params_t *params, const char *option_name, dhcp_opt_t *option){    if(option == NULL) {        ERROR_MESSAGE("unknown option %s to prepend", option_name);        return 1;    }    /* cannot append unless it's a list type. */    if(dhcp_opt_get_type(option) != DHCP_OPT_LIST)  {        ERROR_MESSAGE("user config attempted to prepend to: %s", option_name);        ERROR_MESSAGE("cannot prepend option which is not a list type");        ERROR_MESSAGE("you can only override options which are not lists.");        dhcp_opt_destroy(option);        return 1;    }    list_add(params->prepend_options, option);    return 0;}static int directive_user_defined_option_handler_proc(client_conf_params_t *params, void *directive_data,                                                       directive_group_t group_type, void *group_data,                                                      int var_type){    char *option_name;    list_t *option_data_list;    dhcp_opt_t *option = NULL;    directive_t *directive = directive_data;    uint8_t i;    const char **option_strings = dhcp_option_conf_string_get_array();    option_name = list_first(directive->arguments);    option_data_list = list_get_by_index(directive->arguments, 2);    for(i = 0; i < MAX_OPTIONS_HANDLED; i++) {        if(!strcmp(option_strings[i], option_name)) {            option = dhcp_opt_create_from_user_string(i, option_data_list);            break;        }    }    if(i == MAX_OPTIONS_HANDLED) {        ERROR_MESSAGE("unrecognized dhcp option specified by user config: %s", option_name);        return 1;    }    /* check to make sure option name is valid. */        switch(var_type) {    case CLIENT_VAR_APPEND_OPTIONS:        directive_user_defined_append(params, option_name, option);        break;    case CLIENT_VAR_PREPEND_OPTIONS:        directive_user_defined_prepend(params, option_name, option);        break;    case CLIENT_VAR_OVERRIDE_OPTIONS:        directive_user_defined_override(params, option_name, option);        break;    default:        FATAL_MESSAGE("illegal append/prepend/override specified. i have encountered a bug. i shouldn't be here.");    }    return 0;}static int directive_append_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    return directive_user_defined_option_handler_proc(params, directive_data, group_type, group_data, CLIENT_VAR_APPEND_OPTIONS);}static int directive_prepend_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    return directive_user_defined_option_handler_proc(params, directive_data, group_type, group_data, CLIENT_VAR_PREPEND_OPTIONS);}static int directive_override_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    return directive_user_defined_option_handler_proc(params, directive_data, group_type, group_data, CLIENT_VAR_OVERRIDE_OPTIONS);}static int directive_server_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data){    list_t *args;    const arg_symbol_t *server_symbol;    void *address_data;    void *directives;    client_conf_params_t *new_params;    directive_group_t new_group_type;    directive_t *directive;    directive = directive_data;    args = directive->arguments;    server_symbol = list_first(args);    switch(*server_symbol) {    case CLIENT_SERVER_IP:        /* ip address. */        new_group_type = DIRECTIVE_GROUP_IP_ADDRESS;        break;    case CLIENT_SERVER_MAC:        /* mac address. */        new_group_type = DIRECTIVE_GROUP_MAC_ADDRESS;        break;    default:        FATAL_MESSAGE("illegal server directive group type. this is a bug report me.");        exit(1); /* get rid of pesky compiler warnings. */    }    address_data = list_get_by_index(args, 1);    directives = list_get_by_index(args, 2);    new_params = params_create();    if(directive_group_handler(new_params, directives, new_group_type, address_data) < 0)        return 1;    list_add_to_end(params->lower_params, new_params);    return 0;}static int directive_set_boolean_handler(client_conf_params_t *params, void *directive_data,                                          directive_group_t group_type, void *group_data){    directive_t *directive = directive_data;    list_t *args;    arg_symbol_t *var_symbol;    uint8_t *var_value;    args = directive->arguments;    var_symbol = list_get_by_index(args, 0);    var_value = list_get_by_index(args, 2);    if(client_conf_set_variable_boolean(params, *var_symbol, *var_value)) {        ERROR_MESSAGE("configuration: error unable to set boolean variable %s", var_strings[*var_symbol], var_value);        return 1;    }    return 0;}client_conf_t *create_client_conf(const char *interface){    client_conf_t *cc;    cc = xcalloc(sizeof(client_conf_t));    cc->interface = interface;    cc->conf_file = get_conf_options_fname(cc);    cc->params = params_create(); /* set top level to catchall group. */    cc->params->group_type = DIRECTIVE_GROUP_NULL;    /* setup defaults request options: these we always request */    configure_default_request_options(cc);    if(client_conf_load_options(cc)) {        client_conf_destroy(cc);        return NULL;    }    return cc;}void client_conf_destroy(client_conf_t *cc){    /* destroy loaded parameters. */    params_destroy(cc->params);    /* free up the client conf structure. */    xfree(cc->conf_file);    xfree(cc);    return;}int client_conf_reread(client_conf_t *cc){    if(client_conf_load_options(cc)){        return 1;    }    return 0;}

⌨️ 快捷键说明

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