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

📄 dhcpd.c

📁 DHCP服务器源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* dhcpd.c   DHCP Server Daemon. *//* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") * Copyright (c) 1996-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''. */#ifndef lintstatic char ocopyright[] ="$Id: dhcpd.c,v 1.115.2.12 2004/07/10 00:11:18 dhankins Exp $ Copyright 2004 Internet Systems Consortium.";#endif  static char copyright[] ="Copyright 2004 Internet Systems Consortium.";static char arr [] = "All rights reserved.";static char message [] = "Internet Systems Consortium DHCP Server";static char url [] = "For info, please visit http://www.isc.org/sw/dhcp/";#include "dhcpd.h"#include "version.h"#include <omapip/omapip_p.h>static void usage PROTO ((void));struct iaddr server_identifier;int server_identifier_matched;#if defined (NSUPDATE)/* This stuff is always executed to figure the default values for certain   ddns variables. */char std_nsupdate [] = "						    \n\option server.ddns-hostname =						    \n\  pick (option fqdn.hostname, option host-name);			    \n\option server.ddns-domainname =	config-option domain-name;		    \n\option server.ddns-ttl = encode-int(lease-time / 2, 32);		    \n\option server.ddns-rev-domainname = \"in-addr.arpa.\";";/* This is the old-style name service updater that is executed   whenever a lease is committed.  It does not follow the DHCP-DNS   draft at all. */char old_nsupdate [] = "						    \n\on commit {								    \n\  if (not static and							    \n\      ((config-option server.ddns-updates = null) or			    \n\       (config-option server.ddns-updates != 0))) {			    \n\    set new-ddns-fwd-name =						    \n\      concat (pick (config-option server.ddns-hostname,			    \n\		    option host-name), \".\",				    \n\	      pick (config-option server.ddns-domainname,		    \n\		    config-option domain-name));			    \n\    if (defined (ddns-fwd-name) and ddns-fwd-name != new-ddns-fwd-name) {   \n\      switch (ns-update (delete (IN, A, ddns-fwd-name, leased-address))) {  \n\      case NOERROR:							    \n\	unset ddns-fwd-name;						    \n\	on expiry or release {						    \n\	}								    \n\      }									    \n\    }									    \n\									    \n\    if (not defined (ddns-fwd-name)) {					    \n\      set ddns-fwd-name = new-ddns-fwd-name;				    \n\      if defined (ddns-fwd-name) {					    \n\	switch (ns-update (not exists (IN, A, ddns-fwd-name, null),	    \n\			   add (IN, A, ddns-fwd-name, leased-address,	    \n\				lease-time / 2))) {			    \n\	default:							    \n\	  unset ddns-fwd-name;						    \n\	  break;							    \n\									    \n\	case NOERROR:							    \n\	  set ddns-rev-name =						    \n\	    concat (binary-to-ascii (10, 8, \".\",			    \n\				     reverse (1,			    \n\					      leased-address)), \".\",	    \n\		    pick (config-option server.ddns-rev-domainname,	    \n\			  \"in-addr.arpa.\"));				    \n\	  switch (ns-update (delete (IN, PTR, ddns-rev-name, null),	    \n\			     add (IN, PTR, ddns-rev-name, ddns-fwd-name,    \n\				  lease-time / 2)))			    \n\	    {								    \n\	    default:							    \n\	      unset ddns-rev-name;					    \n\	      on release or expiry {					    \n\		switch (ns-update (delete (IN, A, ddns-fwd-name,	    \n\					   leased-address))) {		    \n\		case NOERROR:						    \n\		  unset ddns-fwd-name;					    \n\		  break;						    \n\		}							    \n\		on release or expiry;					    \n\	      }								    \n\	      break;							    \n\									    \n\	    case NOERROR:						    \n\	      on release or expiry {					    \n\		switch (ns-update (delete (IN, PTR, ddns-rev-name, null))) {\n\		case NOERROR:						    \n\		  unset ddns-rev-name;					    \n\		  break;						    \n\		}							    \n\		switch (ns-update (delete (IN, A, ddns-fwd-name,	    \n\					   leased-address))) {		    \n\		case NOERROR:						    \n\		  unset ddns-fwd-name;					    \n\		  break;						    \n\		}							    \n\		on release or expiry;					    \n\	      }								    \n\	    }								    \n\	}								    \n\      }									    \n\    }									    \n\    unset new-ddns-fwd-name;						    \n\  }									    \n\}";int ddns_update_style;#endif /* NSUPDATE */const char *path_dhcpd_conf = _PATH_DHCPD_CONF;const char *path_dhcpd_db = _PATH_DHCPD_DB;const char *path_dhcpd_pid = _PATH_DHCPD_PID;int dhcp_max_agent_option_packet_length = DHCP_MTU_MAX;static omapi_auth_key_t *omapi_key = (omapi_auth_key_t *)0;int omapi_port;#if defined (TRACING)trace_type_t *trace_srandom;#endifstatic isc_result_t verify_addr (omapi_object_t *l, omapi_addr_t *addr) {	return ISC_R_SUCCESS;}static isc_result_t verify_auth (omapi_object_t *p, omapi_auth_key_t *a) {	if (a != omapi_key)		return ISC_R_INVALIDKEY;	return ISC_R_SUCCESS;}static void omapi_listener_start (void *foo){	omapi_object_t *listener;	isc_result_t result;	listener = (omapi_object_t *)0;	result = omapi_generic_new (&listener, MDL);	if (result != ISC_R_SUCCESS)		log_fatal ("Can't allocate new generic object: %s",			   isc_result_totext (result));	result = omapi_protocol_listen (listener,					(unsigned)omapi_port, 1);	if (result == ISC_R_SUCCESS && omapi_key)		result = omapi_protocol_configure_security			(listener, verify_addr, verify_auth);	if (result != ISC_R_SUCCESS) {		log_error ("Can't start OMAPI protocol: %s",			   isc_result_totext (result));		add_timeout (cur_time + 5, omapi_listener_start, 0, 0, 0);	}	omapi_object_dereference (&listener, MDL);}int main (argc, argv, envp)	int argc;	char **argv, **envp;{	int i, status;	struct servent *ent;	char *s;	int cftest = 0;	int lftest = 0;#ifndef DEBUG	int pidfilewritten = 0;	int pid;	char pbuf [20];	int daemon = 1;#endif	int quiet = 0;	char *server = (char *)0;	isc_result_t result;	unsigned seed;	struct interface_info *ip;	struct parse *parse;	int lose;	omapi_object_t *auth;	struct tsig_key *key;	omapi_typed_data_t *td;	int no_dhcpd_conf = 0;	int no_dhcpd_db = 0;	int no_dhcpd_pid = 0;#if defined (TRACING)	char *traceinfile = (char *)0;	char *traceoutfile = (char *)0;#endif	/* Make sure we have stdin, stdout and stderr. */	status = open ("/dev/null", O_RDWR);	if (status == 0)		status = open ("/dev/null", O_RDWR);	if (status == 1) {		status = open ("/dev/null", O_RDWR);		log_perror = 0; /* No sense logging to /dev/null. */	} else if (status != -1)		close (status);	/* Set up the client classification system. */	classification_setup ();	/* Initialize the omapi system. */	result = omapi_init ();	if (result != ISC_R_SUCCESS)		log_fatal ("Can't initialize OMAPI: %s",			   isc_result_totext (result));	/* Set up the OMAPI wrappers for common objects. */	dhcp_db_objects_setup ();	/* Set up the OMAPI wrappers for various server database internal	   objects. */	dhcp_common_objects_setup ();	/* Initially, log errors to stderr as well as to syslogd. */#ifdef SYSLOG_4_2	openlog ("dhcpd", LOG_NDELAY);	log_priority = DHCPD_LOG_FACILITY;#else	openlog ("dhcpd", LOG_NDELAY, DHCPD_LOG_FACILITY);#endif	for (i = 1; i < argc; i++) {		if (!strcmp (argv [i], "-p")) {			if (++i == argc)				usage ();			for (s = argv [i]; *s; s++)				if (!isdigit (*s))					log_fatal ("%s: not a valid UDP port",					       argv [i]);			status = atoi (argv [i]);			if (status < 1 || status > 65535)				log_fatal ("%s: not a valid UDP port",				       argv [i]);			local_port = htons (status);			log_debug ("binding to user-specified port %d",			       ntohs (local_port));		} else if (!strcmp (argv [i], "-f")) {#ifndef DEBUG			daemon = 0;#endif		} else if (!strcmp (argv [i], "-d")) {#ifndef DEBUG			daemon = 0;#endif			log_perror = -1;		} else if (!strcmp (argv [i], "-s")) {			if (++i == argc)				usage ();			server = argv [i];		} else if (!strcmp (argv [i], "-cf")) {			if (++i == argc)				usage ();			path_dhcpd_conf = argv [i];			no_dhcpd_conf = 1;		} else if (!strcmp (argv [i], "-lf")) {			if (++i == argc)				usage ();			path_dhcpd_db = argv [i];			no_dhcpd_db = 1;		} else if (!strcmp (argv [i], "-pf")) {			if (++i == argc)				usage ();			path_dhcpd_pid = argv [i];			no_dhcpd_pid = 1;                } else if (!strcmp (argv [i], "-t")) {			/* test configurations only */#ifndef DEBUG			daemon = 0;#endif			cftest = 1;			log_perror = -1;                } else if (!strcmp (argv [i], "-T")) {			/* test configurations and lease file only */#ifndef DEBUG			daemon = 0;#endif			cftest = 1;			lftest = 1;			log_perror = -1;		} else if (!strcmp (argv [i], "-q")) {			quiet = 1;			quiet_interface_discovery = 1;		} else if (!strcmp (argv [i], "--version")) {			log_info ("isc-dhcpd-%s", DHCP_VERSION);			exit (0);#if defined (TRACING)		} else if (!strcmp (argv [i], "-tf")) {			if (++i == argc)				usage ();			traceoutfile = argv [i];		} else if (!strcmp (argv [i], "-play")) {			if (++i == argc)				usage ();			traceinfile = argv [i];			trace_replay_init ();#endif /* TRACING */		} else if (argv [i][0] == '-') {			usage ();		} else {			struct interface_info *tmp =				(struct interface_info *)0;			result = interface_allocate (&tmp, MDL);			if (result != ISC_R_SUCCESS)				log_fatal ("Insufficient memory to %s %s: %s",					   "record interface", argv [i],					   isc_result_totext (result));			strcpy (tmp -> name, argv [i]);			if (interfaces) {				interface_reference (&tmp -> next,						     interfaces, MDL);				interface_dereference (&interfaces, MDL);			}			interface_reference (&interfaces, tmp, MDL);			tmp -> flags = INTERFACE_REQUESTED;		}	}	if (!no_dhcpd_conf && (s = getenv ("PATH_DHCPD_CONF"))) {		path_dhcpd_conf = s;	}	if (!no_dhcpd_db && (s = getenv ("PATH_DHCPD_DB"))) {		path_dhcpd_db = s;	}	if (!no_dhcpd_pid && (s = getenv ("PATH_DHCPD_PID"))) {		path_dhcpd_pid = s;	}	if (!quiet) {		log_info ("%s %s", message, DHCP_VERSION);		log_info (copyright);		log_info (arr);		log_info (url);	} else {		quiet = 0;		log_perror = 0;	}#if defined (TRACING)	trace_init (set_time, MDL);	if (traceoutfile)		trace_begin (traceoutfile, MDL);	interface_trace_setup ();	parse_trace_setup ();	trace_srandom = trace_type_register ("random-seed", (void *)0,					     trace_seed_input,					     trace_seed_stop, MDL);#endif

⌨️ 快捷键说明

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