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

📄 dhcp-client-conf.c

📁 this is sample about DHCP-agent
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-client-conf.c,v 1.29 2003/06/27 03:16:27 actmodern Exp $ *  * Copyright 2002 Thamer Alharbash <tmh@whitefang.com> *  * 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. *  */#define MODULE_NAME "dhcp-client-conf"#include "dhcp-local.h"#include "dhcp-limits.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"#include "dhcp-option.h"#include "dhcp-conf.h"#include "dhcp-conf-var.h"#include "dhcp-client-conf.h"#include "dhcp-cache-entry.h"#include "dhcp-client-cache.h"#include "dhcp-options-strings.h"#include "dhcp-client-defaults.h"/* * * * * * * * * * * * * * * * * * * * configuration grammar structures. * * * * * * * * * * * * * * * * * * * *//* * We construct the tree for client-conf code to handle the * grammar for us. * * The one limitation we have now is that the tree has branches * from the top, the command name itself, and everything else is * linear. This is fine for what we want to do now but it may * need to support a more proper grammer tree for more complex * parsing in the future. *//* string arrays for commands. *//* variables the client will accept to be set. */static const char *var_strings[] = {    "hostname",    /* protocol related. */    /* timeout thresholds. */    "dhcp-discover-timeout-threshold",    "dhcp-request-timeout-threshold",    "arp-timeout-threshold",    "icmp-echo-timeout-threshold",    "icmp-subnet-timeout-threshold",    /* retries. */    "dhcp-discover-retries",    "dhcp-request-retries",    "icmp-retries",    "arp-retries",    "default-interface-mtu",    "default-subnet-mask",    /* lease time default percent */    "default-renew-percent",    "default-rebind-percent",};static const char *var_boolean_strings[] = {    "do-measure-router-latency",};/* strings for ip address or mac address with server command. */static const char *server_strings[] = {    "ip-address",    "mac-address",};/* symbol arrays. *//* symbols for string substitution. */static const arg_symbol_t var_symbols[] = {    CLIENT_VAR_HOSTNAME,    CLIENT_VAR_DHCP_DISCOVER_TIMEOUT,    CLIENT_VAR_DHCP_REQUEST_TIMEOUT,    CLIENT_VAR_ARP_TIMEOUT,    CLIENT_VAR_ICMP_ECHO_TIMEOUT,    CLIENT_VAR_ICMP_SUBNET_TIMEOUT,    CLIENT_VAR_DHCP_DISCOVER_RETRIES,    CLIENT_VAR_DHCP_REQUEST_RETRIES,    CLIENT_VAR_ICMP_RETRIES,    CLIENT_VAR_ARP_RETRIES,    CLIENT_VAR_INTERFACE_MTU,    CLIENT_VAR_SUBNET_MASK,    CLIENT_VAR_RENEW_PERCENT,    CLIENT_VAR_REBIND_PERCENT,};static const arg_symbol_t server_symbols[] = {    CLIENT_SERVER_IP,    CLIENT_SERVER_MAC,};static const arg_symbol_t var_boolean_symbols[] = {    CLIENT_VAR_DO_MEASURE_ROUTER_LATENCY_ICMP,};/* argument types, string array pointers, and symbol array pointers */static const arg_type_t     var_boolean_arg_types[] = { CONF_IDENTIFIER, CONF_ASSIGNMENT, CONF_BOOLEAN };static const char         **var_boolean_arg_strings[] = { var_boolean_strings, NULL, NULL };static const arg_symbol_t  *var_boolean_arg_symbols[] = { var_boolean_symbols, NULL, NULL };static const arg_type_t     var_arg_types[] = { CONF_IDENTIFIER, CONF_ASSIGNMENT, CONF_STRING };static const char         **var_arg_strings[] = { var_strings, NULL, NULL };static const arg_symbol_t  *var_arg_symbols[] = { var_symbols, NULL, NULL };static const arg_type_t     request_arg_types[] = { CONF_STRING_LIST };static const char         **request_arg_strings[] = { NULL };static const arg_symbol_t  *request_arg_symbols[] = { NULL };static const arg_type_t     require_arg_types[] = { CONF_STRING_LIST };static const char         **require_arg_strings[] = { NULL };static const arg_symbol_t  *require_arg_symbols[] = { NULL };static const arg_type_t     configure_arg_types[] = { CONF_STRING_LIST };static const char         **configure_arg_strings[] = { NULL };static const arg_symbol_t  *configure_arg_symbols[] = { NULL };static const arg_type_t     append_arg_types[] = { CONF_STRING, CONF_ASSIGNMENT, CONF_STRING_LIST };static const char         **append_arg_strings[] = { NULL, NULL, NULL };static const arg_symbol_t  *append_arg_symbols[] = { NULL, NULL, NULL };static const arg_type_t     prepend_arg_types[] = { CONF_STRING, CONF_ASSIGNMENT, CONF_STRING_LIST };static const char         **prepend_arg_strings[] = { NULL, NULL, NULL };static const arg_symbol_t  *prepend_arg_symbols[] = { NULL, NULL, NULL };static const arg_type_t     override_arg_types[] = { CONF_STRING, CONF_ASSIGNMENT, CONF_STRING_LIST };static const char         **override_arg_strings[] = { NULL, NULL, NULL };static const arg_symbol_t  *override_arg_symbols[] = { NULL, NULL, NULL };static const arg_type_t     server_arg_types[] = { CONF_IDENTIFIER, CONF_ADDRESS, CONF_GROUP };static const char         **server_arg_strings[] = { server_strings, NULL, NULL };static const arg_symbol_t  *server_arg_symbols[] = { server_symbols, NULL, NULL };/* command structures. *//* set command. */static const command_t command_set = {    DIRECTIVE_SET,    "set",    3,    var_arg_strings,    var_arg_types,    var_arg_symbols,};/* set command with boolean. */static const command_t command_set_boolean = {    DIRECTIVE_SET_BOOLEAN,    "enable",    3,    var_boolean_arg_strings,    var_boolean_arg_types,    var_boolean_arg_symbols,};/* request command. */static const command_t command_request = {    DIRECTIVE_REQUEST,    "request",    1,    request_arg_strings,    request_arg_types,    request_arg_symbols,};/* request command. */static const command_t command_require = {    DIRECTIVE_REQUIRE,    "require",    1,    require_arg_strings,    require_arg_types,    require_arg_symbols,};/* configure command. */static const command_t command_configure = {    DIRECTIVE_CONFIGURE,    "configure",    1,    configure_arg_strings,    configure_arg_types,    configure_arg_symbols,};/* server command. */static const command_t command_server = {    DIRECTIVE_SERVER_GROUP,    "server",    3,    server_arg_strings,    server_arg_types,    server_arg_symbols,};/* append command. */static const command_t command_append = {    DIRECTIVE_APPEND,    "append",    3,    append_arg_strings,    append_arg_types,    append_arg_symbols,};/* prepend command. */static const command_t command_prepend = {    DIRECTIVE_PREPEND,    "prepend",    3,    prepend_arg_strings,    prepend_arg_types,    prepend_arg_symbols,};/* prepend command. */static const command_t command_override = {    DIRECTIVE_OVERRIDE,    "override",    3,    override_arg_strings,    override_arg_types,    override_arg_symbols,};static const command_t *commands[] = {    &command_set_boolean,    &command_set,    &command_request,    &command_require,    &command_server,    &command_configure,    &command_append,    &command_prepend,    &command_override,    NULL,};/* forward declaration of directive handlers. */static int directive_group_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_set_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_set_boolean_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_request_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_require_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_server_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_configure_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_append_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_prepend_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);static int directive_override_handler(client_conf_params_t *params, void *directive_data, directive_group_t group_type, void *group_data);/* indexed by directive types in dhcp-client-conf.h . */directive_handler_t directive_handlers[] = {    directive_set_boolean_handler,    directive_set_handler,    directive_request_handler,    directive_require_handler,    directive_server_handler,    directive_configure_handler,    directive_append_handler,    directive_prepend_handler,    directive_override_handler,};/* Basic network configuration.  Here we keep the options we want * by default.  We always want these and will override the config * passed us by the user. */static const int default_config_options[] = {    TAG_DHCP_SUBNET_MASK,           /* Subnet Mask */    TAG_DHCP_ROUTER,                /* Routers. */    TAG_DHCP_INTERFACE_MTU,         /* Interface MTU */    TAG_DHCP_REQUESTED_IP_ADDRESS,  /* Requested IP Address. */    TAG_DHCP_IP_ADDRESS_LEASE_TIME, /* IP Address lease time. */    TAG_DHCP_RENEWAL_TIME,          /* Renewal time. */    TAG_DHCP_REBINDING_TIME,        /* Rebinding time. */    TAG_DHCP_SERVER_IDENTIFIER,     /* Server Identifier. */};/* * * * * * * * * * * * utility routines  * * * * * * * * * * * */static void configure_default_request_options(client_conf_t *cc){    conf_var_t *var;    uint8_t *default_request_options;    int i;    default_request_options = xcalloc(MAX_OPTIONS_HANDLED_NUM * sizeof(uint8_t));    for(i = 0; i < NELMS(default_config_options); i++) {        default_request_options[default_config_options[i]] = 1;    }     var = dhcp_conf_var_create(CLIENT_VAR_REQUEST_OPTIONS, default_request_options);    list_add(cc->params->variables, var);    return;}/* merge two bit arrays into dest. */static void merge_bit_arrays(uint8_t *dest, uint8_t *src, int len){    int i;    for(i = 0; i < len; i++) {        if(src[i])            dest[i] = 1;    }    return;}/* search through a list of conf variables and return if the type * is found.  this is useful for overwriting variables: this is * slow however we assume we're not doing configuration variables * in excess of twenty or so in most situations. */static conf_var_t *find_conf_var(var_type_t var_type, list_t *var_list){    conf_var_t *var;    list_rewind(var_list);    while((var = list_next(var_list)) != NULL)  {        if(var->type == var_type)            return var;    }    return NULL;}/* add variable or replace existing one. */static void add_or_replace_var(list_t *variables, conf_var_t *var){    conf_var_t *cur_var;    /* see if this is already set.*/    cur_var = find_conf_var(var->type, variables);    /* if not add. */    if(cur_var == NULL) {        list_add(variables, var);    } else {        /* otherwise overwrite. */        dhcp_conf_var_overwrite(cur_var, var->type, var->val);        dhcp_conf_var_destroy_no_val_free(var); /* we need to keep the value so don't free val. */    }}/* add or overwrite the bit array option. */static void add_or_overwrite_bit_array_var(list_t *variables, uint8_t *new_array, var_type_t var_type){    conf_var_t *cur_var;    uint8_t *old_array;    /* see if this is already set.*/    cur_var = find_conf_var(var_type, variables);    /* if not add. */    if(cur_var == NULL) {        cur_var = dhcp_conf_var_create(var_type, new_array);        list_add(variables, cur_var);    } else {        old_array = dhcp_conf_var_get_val(cur_var);        merge_bit_arrays(old_array, new_array, MAX_OPTIONS_HANDLED_NUM);        xfree(new_array); /* free up since we're not replacing with it. */

⌨️ 快捷键说明

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