dhcp-sysconf.c

来自「this is sample about DHCP-agent」· C语言 代码 · 共 241 行

C
241
字号
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-sysconf.c,v 1.22 2003/06/27 20:59:17 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. *  *//* Configure host internal structures. */#define MODULE_NAME "dhcp-sysconf"#include "dhcp-local.h"#include "dhcp-limits.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"#include "dhcp-option.h"#include "dhcp-options-strings.h"#include "dhcp-cache-entry.h"#include "dhcp-client-cache.h"#include "dhcp-client-conf.h"#include "dhcp-client.h"#include "dhcp-sysconf.h"#include "dhcp-client-guile.h"/* override, prepend, and append to options. *//* do overrides. */static void sysconf_override(list_t *options, list_t *overrides){    dhcp_opt_t *orig, *override;    /* walk down list and override where necessary.  since we're    * overriding the options list and will be rewriting it we    * need to loop on overrides in order to avoid looping    * indefinetly. unfortunately this gives us the worest case    * scenario in terms of cpu time. FIXME: write a    * list_replace_datum routine */    list_rewind(overrides);    while((override = list_next(overrides)) != NULL) {        list_rewind(options);        while((orig = list_next(options)) != NULL) {            if(dhcp_opt_get_tag(override) == dhcp_opt_get_tag(orig)) {                list_remove_by_datum(options, orig);                dhcp_opt_destroy(orig);                list_add(options, dhcp_option_copy(override));                break;            }        }    }    return;}static void sysconf_prepend_or_append_proc(list_t *options, list_t *add_options,                                           void (*add)(dhcp_opt_t *orig, dhcp_opt_t *add)){    dhcp_opt_t *orig, *add_opt;    /* walk down list and do addition where necessary. */    list_rewind(options);    while((orig = list_next(options)) != NULL) {        list_rewind(add_options);        while((add_opt = list_next(add_options)) != NULL) {            if(dhcp_opt_get_tag(orig) == dhcp_opt_get_tag(add_opt)) {                /* we already know that the conf code guarantees                 * we're only dealing with list types. no need to                 * double check here. */                add(orig, add_opt);                break;            }        }    }    return;}/* do prepends. */static void sysconf_prepend(list_t *options, list_t *prepends){    sysconf_prepend_or_append_proc(options, prepends, dhcp_option_prepend);    return;}/* do appends. */static void sysconf_append(list_t *options, list_t *appends){    sysconf_prepend_or_append_proc(options, appends, dhcp_option_append);    return;}/* * * * * * * * * * * * * Sysconf interface.  * * * * * * * * * * * * */void do_sysconf(list_t *options, dhcp_client_control_t *dc, int state){    list_t *overrides, *prepends, *appends;    overrides = client_conf_get_override_options(dc->conf,                                                 dhcp_client_get_server_ip_address(dc),                                                 dhcp_client_get_server_hw_address(dc));    sysconf_override(options, overrides);    prepends = client_conf_get_append_options(dc->conf,                                              dhcp_client_get_server_ip_address(dc),                                              dhcp_client_get_server_hw_address(dc));    sysconf_prepend(options, prepends);    appends = client_conf_get_prepend_options(dc->conf,                                              dhcp_client_get_server_ip_address(dc),                                              dhcp_client_get_server_hw_address(dc));    sysconf_append(options, appends);                                                            return dhcp_sysconf_guile(dc, options, state);}/* setup timers only. this is done internally after rebinding, * renewing or acquiring a new lease. */void do_sysconf_setup_timers(list_t *options, dhcp_client_control_t *dc){    dhcp_opt_t *option;    dhcp_opt_tag_t tag;    uint32_t *secs;    uint32_t renew_time = 0;    uint32_t rebind_time = 0;    uint32_t lease_time = 0;    uint8_t have_renew = 0;    uint8_t have_rebind = 0;    uint8_t have_lease_time = 0;    float percent;    reinitialize_timer(dc->context->timer);    list_rewind(options);    while((option = list_next(options)) != NULL) {        tag = dhcp_opt_get_tag(option);        if(tag == TAG_DHCP_RENEWAL_TIME) {            secs = dhcp_opt_get_host_data(option);            have_renew = 1;            renew_time = *secs;            continue;        } else if(tag == TAG_DHCP_REBINDING_TIME) {            secs = dhcp_opt_get_host_data(option);            have_rebind = 1;            rebind_time = *secs;            continue;        } else if(tag == TAG_DHCP_IP_ADDRESS_LEASE_TIME) {            secs = dhcp_opt_get_host_data(option);            have_lease_time = 1;            lease_time = *secs;            continue;        }    }    if(have_lease_time != 1) {        /* we don't have an actual lease time.  check         * configuration options on what we should do. */        WARN_MESSAGE("no lease time passed. assuming lease will never expire.");        lease_time = 0xffffffff;    }    /* a lease time of 0xffffffff is defined by the RFC2131 as infinite. */    if(lease_time == INFINITE_TIME) {        INFO_MESSAGE("setting infinite lease time.");        dhcp_client_lease_time_is_infinite(dc);    } else {        dhcp_client_lease_time_is_finite(dc);        INFO_MESSAGE("setting lease time to: %"PRIu32" seconds", lease_time);        timer_add_trigger(dc->context->timer, lease_time, TIMER_IP_LEASE);        if(!have_renew) {            percent = (float)client_conf_get_renew_percent(dc->conf)/100;            INFO_MESSAGE("no renew time passed. using %%%.2f percent of lease time as renew time.", percent);            renew_time = lease_time * percent;        }        INFO_MESSAGE("setting renew time to: %"PRIu32" seconds", renew_time);        timer_add_trigger(dc->context->timer, renew_time, TIMER_RENEW);        if(!have_rebind) {            percent = (float)client_conf_get_rebind_percent(dc->conf)/100;            INFO_MESSAGE("no rebind time passed. using %%%.2f percent of lease time as rebind time.", percent);            rebind_time = lease_time * percent;        }        INFO_MESSAGE("setting rebind time to: %"PRIu32" seconds", rebind_time);        timer_add_trigger(dc->context->timer, rebind_time, TIMER_REBIND);    }    return;}

⌨️ 快捷键说明

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