📄 dhcp-server-conf.c
字号:
return 1; } directives = conf_get_directives(conf); list_rewind(directives); while((directive = list_next(directives)) != NULL) { /* read in the compiled directives. */ if(directive_handlers[directive->command_code](sc, directive, GROUP_NULL, NULL)) return 1; } return 0;}/* * * * * * * * * * * * * directive handlers. * * * * * * * * * * * * *//* variable set handler. */static int directive_set_handler(server_conf_t *server_conf, directive_t *directive, int group_type, void *group_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(server_conf_set_variable(server_conf, *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;}static int directive_lease_handler_proc(server_conf_t *server_conf, directive_t *directive, int group_type, void *group_data, list_t *option_list){ directive_t *lower_directive; list_t *lease_directives, *args; /* arguments. */ args = directive->arguments; /* parse lower directives. */ lease_directives = list_get_by_index(args, 2); list_rewind(lease_directives); while((lower_directive = list_next(lease_directives)) != NULL) { switch(lower_directive->command_code) { case DIRECTIVE_OPTION: if(directive_option_handler(server_conf, lower_directive, group_type, option_list)) return 1; break; default: ERROR_MESSAGE("illegal directive specified in a range lease definition"); } } return 0;}/* lease range handler */static int directive_range_handler(server_conf_t *server_conf, directive_t *directive, int group_type, void *group_data){ lease_definition_t *lease_def; ip_addr_t bottom_address, top_address; list_t *args, *address_pair, *option_list; uint32_t lease_expiry = 0; uint32_t renew_time = 0; uint32_t rebind_time = 0; /* arguments. */ args = directive->arguments; /* get address pair, and create pair. */ bottom_address = *(ip_addr_t *)list_first(args); top_address = *(ip_addr_t *)list_second(args); /* make sure address range is correct. */ if(ntohl(bottom_address) >= ntohl(top_address)) { ERROR_MESSAGE("range lease defined with bottom address higher or equal to top address."); return 1; } address_pair = list_create(); list_add(address_pair, &top_address); list_add_to_end(address_pair, &bottom_address); /* create option handler by calling the general lease handler. */ option_list = list_create(); directive_lease_handler_proc(server_conf, directive, GROUP_LEASE_DEF, group_data, option_list); lease_def = lease_definition_create(NULL, LEASE_RANGE_ADDRESS, address_pair, option_list, lease_expiry, renew_time, rebind_time); list_add(server_conf->lease_defs, lease_def); /* clean up. */ list_destroy(address_pair, NULL); return 0;}/* hostname handler. */static int directive_hostname_handler(server_conf_t *server_conf, directive_t *directive, int group_type, void *group_data){ lease_constraint_t *constraint; list_t *args, *option_list; char *hostname; ip_addr_t *ip_address; lease_definition_t *lease_def; uint32_t lease_expiry = 0; uint32_t renew_time = 0; uint32_t rebind_time = 0; /* arguments. */ args = directive->arguments; /* first argument is the hostname. */ hostname = list_first(args); if((constraint = lease_constraint_create(LEASE_CONSTRAINT_HOSTNAME, hostname)) == NULL) { ERROR_MESSAGE("illegal hostname constraint specified: %s", hostname); return 1; } ip_address = list_second(args); option_list = list_create(); directive_lease_handler_proc(server_conf, directive, GROUP_LEASE_DEF, group_data, option_list); lease_def = lease_definition_create(constraint, LEASE_SINGLE_ADDRESS, ip_address, option_list, lease_expiry, renew_time, rebind_time); list_add(server_conf->lease_defs, lease_def); return 0;}/* hardware address handler. */static int directive_hw_address_handler(server_conf_t *server_conf, directive_t *directive, int group_type, void *group_data){ lease_constraint_t *constraint; list_t *args, *option_list; eth_addr_t *eth_addr; char *eth_addr_string; ip_addr_t *ip_address; lease_definition_t *lease_def; uint32_t lease_expiry = 0; uint32_t renew_time = 0; uint32_t rebind_time = 0; /* arguments. */ args = directive->arguments; /* first argument is the hostname. */ eth_addr = list_first(args); if((constraint = lease_constraint_create(LEASE_CONSTRAINT_HARDWARE_ADDRESS, eth_addr)) == NULL) { eth_addr_string = eth_addr_to_string(*eth_addr); ERROR_MESSAGE("illegal mac address constraint specified: %s", eth_addr_string); xfree(eth_addr_string); return 1; } ip_address = list_second(args); option_list = list_create(); directive_lease_handler_proc(server_conf, directive, GROUP_LEASE_DEF, group_data, option_list); lease_def = lease_definition_create(constraint, LEASE_SINGLE_ADDRESS, ip_address, option_list, lease_expiry, renew_time, rebind_time); list_add(server_conf->lease_defs, lease_def); return 0;}/* lease option handler. */static int directive_option_handler(server_conf_t *server_conf, directive_t *directive, int group_type, void *group_data){ dhcp_opt_t *option = NULL; /* get rid of compiler warning. */ list_t *option_list, *option_data_list; char *option_name; uint8_t i; const char **option_strings; if(group_type == GROUP_NULL) { ERROR_MESSAGE("dhcp option setting found outside of lease definition"); return 1; } option_name = list_first(directive->arguments); option_data_list = list_get_by_index(directive->arguments, 2); option_strings = dhcp_option_conf_string_get_array(); for(i = 0; i < MAX_OPTIONS_HANDLED; i++) { if(!strcmp(option_strings[i], option_name)) { if((option = dhcp_opt_create_from_user_string(i, option_data_list)) == NULL) { ERROR_MESSAGE("illegal specification for option %s", option_name); return 1; } break; } } if(i == MAX_OPTIONS_HANDLED) { ERROR_MESSAGE("unrecognized dhcp option specified by user config: %s", option_name); return 1; } option_list = group_data; list_add_to_end(option_list, option); return 0;}/* * * * * * * * * * * * public interface * * * * * * * * * * * */server_conf_t *server_conf_create(const char *interface){ server_conf_t *sc; sc = xcalloc(sizeof(server_conf_t)); sc->interface = interface; sc->conf_file = get_conf_options_fname(interface); /* setup lease definitions. */ sc->lease_defs = list_create(); /* set defaults. */ sc->default_rebind_percent = SERVER_DEFAULT_REBIND_PERCENT; sc->default_renew_percent = SERVER_DEFAULT_RENEW_PERCENT; sc->poll_timeout = SERVER_DEFAULT_POLL_TIMEOUT; if(server_conf_load_options(sc)) { server_conf_destroy(sc); return NULL; } return sc;}void server_conf_destroy(server_conf_t *sc){ /* not implemented yet. */ return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -