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

📄 omapi.c

📁 DHCP服务器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* omapi.c   OMAPI object interfaces for the DHCP server. *//* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") * Copyright (c) 1999-2003 by Internet Software Consortium * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * *   Internet Systems Consortium, Inc. *   950 Charter Street *   Redwood City, CA 94063 *   <info@isc.org> *   http://www.isc.org/ * * This software has been written for Internet Systems Consortium * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc. * To learn more about Internet Systems Consortium, see * ``http://www.isc.org/''.  To learn more about Vixie Enterprises, * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see * ``http://www.nominum.com''. *//* Many, many thanks to Brian Murrell and BCtel for this code - BCtel   provided the funding that resulted in this code and the entire   OMAPI support library being written, and Brian helped brainstorm   and refine the requirements.  To the extent that this code is   useful, you have Brian and BCtel to thank.  Any limitations in the   code are a result of mistakes on my part.  -- Ted Lemon */#ifndef lintstatic char copyright[] ="$Id: omapi.c,v 1.46.2.17 2004/06/10 17:59:57 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium.  All rights reserved.\n";#endif /* not lint */#include "dhcpd.h"#include <omapip/omapip_p.h>omapi_object_type_t *dhcp_type_lease;omapi_object_type_t *dhcp_type_pool;omapi_object_type_t *dhcp_type_class;omapi_object_type_t *dhcp_type_subclass;omapi_object_type_t *dhcp_type_host;#if defined (FAILOVER_PROTOCOL)omapi_object_type_t *dhcp_type_failover_state;omapi_object_type_t *dhcp_type_failover_link;omapi_object_type_t *dhcp_type_failover_listener;#endifvoid dhcp_db_objects_setup (){	isc_result_t status;	status = omapi_object_type_register (&dhcp_type_lease,					     "lease",					     dhcp_lease_set_value,					     dhcp_lease_get_value,					     dhcp_lease_destroy,					     dhcp_lease_signal_handler,					     dhcp_lease_stuff_values,					     dhcp_lease_lookup, 					     dhcp_lease_create,					     dhcp_lease_remove,#if defined (COMPACT_LEASES)					     dhcp_lease_free,					     dhcp_lease_get,#else					     0, 0,#endif					     0,					     sizeof (struct lease),					     0, RC_LEASE);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register lease object type: %s",			   isc_result_totext (status));	status = omapi_object_type_register (&dhcp_type_class,					     "class",					     dhcp_class_set_value,					     dhcp_class_get_value,					     dhcp_class_destroy,					     dhcp_class_signal_handler,					     dhcp_class_stuff_values,					     dhcp_class_lookup, 					     dhcp_class_create,					     dhcp_class_remove, 0, 0, 0,					     sizeof (struct class), 0,					     RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register class object type: %s",			   isc_result_totext (status));	status = omapi_object_type_register (&dhcp_type_subclass,					     "subclass",					     dhcp_subclass_set_value,					     dhcp_subclass_get_value,					     dhcp_class_destroy,					     dhcp_subclass_signal_handler,					     dhcp_subclass_stuff_values,					     dhcp_subclass_lookup, 					     dhcp_subclass_create,					     dhcp_subclass_remove, 0, 0, 0,					     sizeof (struct class), 0, RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register subclass object type: %s",			   isc_result_totext (status));	status = omapi_object_type_register (&dhcp_type_pool,					     "pool",					     dhcp_pool_set_value,					     dhcp_pool_get_value,					     dhcp_pool_destroy,					     dhcp_pool_signal_handler,					     dhcp_pool_stuff_values,					     dhcp_pool_lookup, 					     dhcp_pool_create,					     dhcp_pool_remove, 0, 0, 0,					     sizeof (struct pool), 0, RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register pool object type: %s",			   isc_result_totext (status));	status = omapi_object_type_register (&dhcp_type_host,					     "host",					     dhcp_host_set_value,					     dhcp_host_get_value,					     dhcp_host_destroy,					     dhcp_host_signal_handler,					     dhcp_host_stuff_values,					     dhcp_host_lookup, 					     dhcp_host_create,					     dhcp_host_remove, 0, 0, 0,					     sizeof (struct host_decl),					     0, RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register host object type: %s",			   isc_result_totext (status));#if defined (FAILOVER_PROTOCOL)	status = omapi_object_type_register (&dhcp_type_failover_state,					     "failover-state",					     dhcp_failover_state_set_value,					     dhcp_failover_state_get_value,					     dhcp_failover_state_destroy,					     dhcp_failover_state_signal,					     dhcp_failover_state_stuff,					     dhcp_failover_state_lookup, 					     dhcp_failover_state_create,					     dhcp_failover_state_remove,					     0, 0, 0,					     sizeof (dhcp_failover_state_t),					     0, RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register failover state object type: %s",			   isc_result_totext (status));	status = omapi_object_type_register (&dhcp_type_failover_link,					     "failover-link",					     dhcp_failover_link_set_value,					     dhcp_failover_link_get_value,					     dhcp_failover_link_destroy,					     dhcp_failover_link_signal,					     dhcp_failover_link_stuff_values,					     0, 0, 0, 0, 0, 0,					     sizeof (dhcp_failover_link_t), 0,					     RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register failover link object type: %s",			   isc_result_totext (status));	status = omapi_object_type_register (&dhcp_type_failover_listener,					     "failover-listener",					     dhcp_failover_listener_set_value,					     dhcp_failover_listener_get_value,					     dhcp_failover_listener_destroy,					     dhcp_failover_listener_signal,					     dhcp_failover_listener_stuff,					     0, 0, 0, 0, 0, 0,					     sizeof					     (dhcp_failover_listener_t), 0,					     RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register failover listener object type: %s",			   isc_result_totext (status));#endif /* FAILOVER_PROTOCOL */}isc_result_t dhcp_lease_set_value  (omapi_object_t *h,				    omapi_object_t *id,				    omapi_data_string_t *name,				    omapi_typed_data_t *value){	struct lease *lease;	isc_result_t status;	int foo;	if (h -> type != dhcp_type_lease)		return ISC_R_INVALIDARG;	lease = (struct lease *)h;	/* We're skipping a lot of things it might be interesting to	   set - for now, we just make it possible to whack the state. */	if (!omapi_ds_strcmp (name, "state")) {	    unsigned long bar;	    const char *ols, *nls;	    status = omapi_get_int_value (&bar, value);	    if (status != ISC_R_SUCCESS)		return status;	    	    if (bar < 1 || bar > FTS_LAST)		return ISC_R_INVALIDARG;	    nls = binding_state_names [bar - 1];	    if (lease -> binding_state >= 1 &&		lease -> binding_state <= FTS_LAST)		ols = binding_state_names [lease -> binding_state - 1];	    else		ols = "unknown state";	    	    if (lease -> binding_state != bar) {		lease -> next_binding_state = bar;		if (supersede_lease (lease, 0, 1, 1, 1)) {			log_info ("lease %s state changed from %s to %s",				  piaddr(lease->ip_addr), ols, nls);			return ISC_R_SUCCESS;		}		log_info ("lease state change from %s to %s failed.",			  ols, nls);		return ISC_R_IOERROR;	    }	    return ISC_R_UNCHANGED;	} else if (!omapi_ds_strcmp (name, "ip-address")) {	    return ISC_R_NOPERM;	} else if (!omapi_ds_strcmp (name, "dhcp-client-identifier")) {	    return ISC_R_UNCHANGED;	/* XXX take change. */	} else if (!omapi_ds_strcmp (name, "hostname")) {	    return ISC_R_UNCHANGED;	/* XXX take change. */	} else if (!omapi_ds_strcmp (name, "client-hostname")) {	    return ISC_R_UNCHANGED;	/* XXX take change. */	} else if (!omapi_ds_strcmp (name, "host")) {	    return ISC_R_UNCHANGED;	/* XXX take change. */	} else if (!omapi_ds_strcmp (name, "subnet")) {	    return ISC_R_INVALIDARG;	} else if (!omapi_ds_strcmp (name, "pool")) {	    return ISC_R_NOPERM;	} else if (!omapi_ds_strcmp (name, "starts")) {	    return ISC_R_NOPERM;	} else if (!omapi_ds_strcmp (name, "ends")) {	    return ISC_R_NOPERM;	} else if (!omapi_ds_strcmp (name, "billing-class")) {	    return ISC_R_UNCHANGED;	/* XXX carefully allow change. */	} else if (!omapi_ds_strcmp (name, "hardware-address")) {	    return ISC_R_UNCHANGED;	/* XXX take change. */	} else if (!omapi_ds_strcmp (name, "hardware-type")) {	    return ISC_R_UNCHANGED;	/* XXX take change. */	} else if (lease -> scope) {	    status = binding_scope_set_value (lease -> scope, 0, name, value);	    if (status == ISC_R_SUCCESS) {		    if (write_lease (lease) && commit_leases ())			    return ISC_R_SUCCESS;		    return ISC_R_IOERROR;	    }	}	/* Try to find some inner object that can take the value. */	if (h -> inner && h -> inner -> type -> set_value) {		status = ((*(h -> inner -> type -> set_value))			  (h -> inner, id, name, value));		if (status == ISC_R_SUCCESS || status == ISC_R_UNCHANGED)			return status;	}			  	if (!lease -> scope) {		if (!binding_scope_allocate (&lease -> scope, MDL))			return ISC_R_NOMEMORY;	}	status = binding_scope_set_value (lease -> scope, 1, name, value);	if (status != ISC_R_SUCCESS)		return status;	if (write_lease (lease) && commit_leases ())		return ISC_R_SUCCESS;	return ISC_R_IOERROR;}isc_result_t dhcp_lease_get_value (omapi_object_t *h, omapi_object_t *id,				   omapi_data_string_t *name,				   omapi_value_t **value){	struct lease *lease;	isc_result_t status;	if (h -> type != dhcp_type_lease)		return ISC_R_INVALIDARG;	lease = (struct lease *)h;	if (!omapi_ds_strcmp (name, "state"))		return omapi_make_int_value (value, name,					     (int)lease -> binding_state, MDL);	else if (!omapi_ds_strcmp (name, "ip-address"))		return omapi_make_const_value (value, name,					       lease -> ip_addr.iabuf,					       lease -> ip_addr.len, MDL);	else if (!omapi_ds_strcmp (name, "dhcp-client-identifier")) {		return omapi_make_const_value (value, name,					       lease -> uid,					       lease -> uid_len, MDL);	} else if (!omapi_ds_strcmp (name, "client-hostname")) {		if (lease -> client_hostname)			return omapi_make_string_value				(value, name, lease -> client_hostname, MDL);		return ISC_R_NOTFOUND;	} else if (!omapi_ds_strcmp (name, "host")) {		if (lease -> host)			return omapi_make_handle_value				(value, name,				 ((omapi_object_t *)lease -> host), MDL);	} else if (!omapi_ds_strcmp (name, "subnet"))		return omapi_make_handle_value (value, name,						((omapi_object_t *)						 lease -> subnet), MDL);	else if (!omapi_ds_strcmp (name, "pool"))		return omapi_make_handle_value (value, name,						((omapi_object_t *)						 lease -> pool), MDL);	else if (!omapi_ds_strcmp (name, "billing-class")) {		if (lease -> billing_class)			return omapi_make_handle_value				(value, name,				 ((omapi_object_t *)lease -> billing_class),				 MDL);		return ISC_R_NOTFOUND;	} else if (!omapi_ds_strcmp (name, "hardware-address")) {		if (lease -> hardware_addr.hlen)			return omapi_make_const_value				(value, name, &lease -> hardware_addr.hbuf [1],				 (unsigned)(lease -> hardware_addr.hlen - 1),				 MDL);		return ISC_R_NOTFOUND;	} else if (!omapi_ds_strcmp (name, "hardware-type")) {		if (lease -> hardware_addr.hlen)			return omapi_make_int_value				(value, name, lease -> hardware_addr.hbuf [0],				 MDL);		return ISC_R_NOTFOUND;	} else if (lease -> scope) {		status = binding_scope_get_value (value, lease -> scope, name);		if (status != ISC_R_NOTFOUND)			return status;	}	/* Try to find some inner object that can take the value. */	if (h -> inner && h -> inner -> type -> get_value) {		status = ((*(h -> inner -> type -> get_value))			  (h -> inner, id, name, value));		if (status == ISC_R_SUCCESS)			return status;	}	return ISC_R_UNKNOWNATTRIBUTE;}isc_result_t dhcp_lease_destroy (omapi_object_t *h, const char *file, int line){	struct lease *lease;	isc_result_t status;	if (h -> type != dhcp_type_lease)		return ISC_R_INVALIDARG;	lease = (struct lease *)h;	if (lease -> uid)		uid_hash_delete (lease);	hw_hash_delete (lease);	if (lease -> on_release)		executable_statement_dereference (&lease -> on_release,						  file, line);	if (lease -> on_expiry)		executable_statement_dereference (&lease -> on_expiry,						  file, line);	if (lease -> on_commit)		executable_statement_dereference (&lease -> on_commit,						  file, line);	if (lease -> scope)		binding_scope_dereference (&lease -> scope, file, line);	if (lease -> agent_options)		option_chain_head_dereference (&lease -> agent_options,					       file, line);	if (lease -> uid && lease -> uid != lease -> uid_buf) {		dfree (lease -> uid, MDL);		lease -> uid = &lease -> uid_buf [0];		lease -> uid_len = 0;	}	if (lease -> client_hostname) {		dfree (lease -> client_hostname, MDL);		lease -> client_hostname = (char *)0;	}	if (lease -> host)		host_dereference (&lease -> host, file, line);	if (lease -> subnet)		subnet_dereference (&lease -> subnet, file, line);	if (lease -> pool)		pool_dereference (&lease -> pool, file, line);	if (lease -> state) {		free_lease_state (lease -> state, file, line);		lease -> state = (struct lease_state *)0;		cancel_timeout (lease_ping_timeout, lease);		--outstanding_pings; /* XXX */	}	if (lease -> billing_class)		class_dereference			(&lease -> billing_class, file, line);#if defined (DEBUG_MEMORY_LEAKAGE) || \		defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)	/* XXX we should never be destroying a lease with a next	   XXX pointer except on exit... */	if (lease -> next)		lease_dereference (&lease -> next, file, line);	if (lease -> n_hw)		lease_dereference (&lease -> n_hw, file, line);	if (lease -> n_uid)		lease_dereference (&lease -> n_uid, file, line);	if (lease -> next_pending)		lease_dereference (&lease -> next_pending, file, line);

⌨️ 快捷键说明

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