dhcp-lease.c
来自「this is sample about DHCP-agent」· C语言 代码 · 共 318 行
C
318 行
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-lease.c,v 1.6 2003/07/14 05:58:17 actmodern Exp $ * * Copyright 2002 Thamer Alharbash * * 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-lease"#include "dhcp-local.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"#include "dhcp-lease.h"#include "dhcp-option.h"#include "dhcp-options-strings.h"/* * * * * * * * * * * * public interface. * * * * * * * * * * * *//* create a constraint for a lease based on constraint type, and data passed. */lease_constraint_t *lease_constraint_create(int constraint_type, void *data){ char *hostname; lease_constraint_t *constraint; constraint = xcalloc(sizeof(lease_constraint_t)); constraint->constraint_type = constraint_type; switch(constraint_type) { case LEASE_CONSTRAINT_HARDWARE_ADDRESS: memcpy(&constraint->data.hw_address.data, data, ETH_ADDR_LEN); break; case LEASE_CONSTRAINT_HOSTNAME: hostname = data; constraint->data.hostname = xstrdup(hostname); break; default: FATAL_MESSAGE("illegal constraint type passed. this is a bug report me."); } return constraint;}/* destroy a lease constraint. */void lease_constraint_destroy(lease_constraint_t *constraint){ switch(constraint->constraint_type) { case LEASE_CONSTRAINT_HOSTNAME: xfree(constraint->data.hostname); break; case LEASE_CONSTRAINT_HARDWARE_ADDRESS: /* fall through. */ case LEASE_CONSTRAINT_NONE: /* fall through. */ default: break; } xfree(constraint); return;}lease_definition_t *lease_definition_create(lease_constraint_t *constraint, int lease_type, void *data, list_t *options, uint32_t lease_expiry, uint32_t renew_time, uint32_t rebind_time){ lease_definition_t *lease_def; list_t *addr_range; lease_def = xcalloc(sizeof(lease_definition_t)); /* assign type. */ lease_def->lease_type = lease_type; /* assign constraint. */ lease_def->constraint = constraint; /* setup addresses. */ switch(lease_type) { case LEASE_SINGLE_ADDRESS: lease_def->address_info.address = *(ip_addr_t *)data; break; case LEASE_RANGE_ADDRESS: addr_range = data; lease_def->address_info.address_range.top_range = *(ip_addr_t *)list_first(addr_range); lease_def->address_info.address_range.bottom_range = *(ip_addr_t *)list_second(addr_range); break; default: FATAL_MESSAGE("invalid lease type specified for creation. this is a bug. report me."); } /* options we'll pass. */ lease_def->options = options; /* FIXME: set renew/rebind to defaults if they are passed as zero. */ lease_def->lease_expiry = lease_expiry; lease_def->renew_time = lease_expiry; lease_def->rebind_time = lease_expiry; return lease_def;}void lease_definition_destroy(lease_definition_t *lease_def){ if(lease_def->constraint != NULL) lease_constraint_destroy(lease_def->constraint); dhcp_opt_destroy_option_list(lease_def->options); xfree(lease_def); return;}/* 2string arrays: all indexed against enums in dhcp-lease.h *//* constraint type to string. */static const char *lease_constraint2string[] = { "None", "Hardware Address", "Hostname",};/* lease type to string. */static const char *lease_type2string[] = { "Single Address", "Range Address",};/* * * * * * * * * accessors. * * * * * * * * *//* lease constraint. */const char *lease_constraint_type_to_string(lease_constraint_t *constraint){ int constraint_type = constraint->constraint_type; if(constraint_type >= NELMS(lease_constraint2string)) return "Out of bounds."; return lease_constraint2string[constraint_type];}int lease_constraint_get_type(lease_constraint_t *constraint){ return constraint->constraint_type;}const char *lease_constraint_get_hostname(lease_constraint_t *constraint){ return constraint->data.hostname;}eth_addr_t lease_constraint_get_hw_address(lease_constraint_t *constraint){ return constraint->data.hw_address;}/* lease definition. */const char *lease_type_to_string(lease_definition_t *lease){ int lease_type = lease->lease_type; if(lease_type >= NELMS(lease_type2string)) return "Out of bounds."; return lease_type2string[lease_type];}int lease_definition_get_type(lease_definition_t *lease){ return lease->lease_type;}lease_constraint_t *lease_definition_get_constraint(lease_definition_t *lease){ return lease->constraint;}ip_addr_t lease_definition_get_bottom_addr(lease_definition_t *lease){ return lease->address_info.address_range.bottom_range;}ip_addr_t lease_definition_get_top_addr(lease_definition_t *lease){ return lease->address_info.address_range.top_range;}ip_addr_t lease_definition_get_addr(lease_definition_t *lease){ return lease->address_info.address;}/* * * * * * * * * * * misc utilities. * * * * * * * * * * */void pretty_print_lease_def(lease_definition_t *lease_def){ char *hw_address; const char *opt_name; char *opt_val; dhcp_opt_t *option; char *bottom_addr, *top_addr; INFO_MESSAGE("Lease type: %s", lease_type_to_string(lease_def)); switch(lease_definition_get_type(lease_def)) { case LEASE_SINGLE_ADDRESS: top_addr = ip_addr_to_string(lease_definition_get_addr(lease_def)); INFO_MESSAGE("Address: %s", top_addr); xfree(top_addr); break; case LEASE_RANGE_ADDRESS: bottom_addr = ip_addr_to_string(lease_definition_get_bottom_addr(lease_def)); top_addr = ip_addr_to_string(lease_definition_get_top_addr(lease_def)); INFO_MESSAGE("Address Range: %s to %s", bottom_addr, top_addr); xfree(bottom_addr); xfree(top_addr); break; default: FATAL_MESSAGE("illegal lease type specified. this is a bug. report me."); } if(lease_def->constraint == NULL) { INFO_MESSAGE("Lease constraint type: None."); } else { INFO_MESSAGE("Lease constraint type: %s", lease_constraint_type_to_string(lease_def->constraint)); switch(lease_def->constraint->constraint_type) { case LEASE_CONSTRAINT_NONE: break; case LEASE_CONSTRAINT_HARDWARE_ADDRESS: hw_address = eth_addr_to_string(lease_def->constraint->data.hw_address); INFO_MESSAGE("Hardware Address: %s", hw_address); xfree(hw_address); break; case LEASE_CONSTRAINT_HOSTNAME: INFO_MESSAGE("Hostname: %s", lease_def->constraint->data.hostname); break; default: FATAL_MESSAGE("illegal constraint type specified. this is a bug. report me."); } } INFO_MESSAGE(" "); INFO_MESSAGE("Lease options:"); INFO_MESSAGE(" "); if(list_get_len(lease_def->options) == 0) { INFO_MESSAGE("No options specified."); } else { list_rewind(lease_def->options); while((option = list_next(lease_def->options)) != NULL) { opt_name = dhcp_option_printable_string_get(dhcp_opt_get_tag(option)); opt_val = dhcp_opt_get_user_string(option); INFO_MESSAGE("%s = %s", opt_name, opt_val); xfree(opt_val); } } INFO_MESSAGE(" "); return;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?