dhcpd.c

来自「open source dhcp server client etc...」· C语言 代码 · 共 1,171 行 · 第 1/3 页

C
1,171
字号
/* dhcpd.c   DHCP Server Daemon. *//* * Copyright (c) 1996-2001 Internet Software Consortium. * All rights reserved. * * 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. Neither the name of Internet Software Consortium nor the names *    of its contributors may be used to endorse or promote products derived *    from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * This software has been written for the Internet Software Consortium * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc. * To learn more about the Internet Software 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.6 2001/06/22 02:12:58 mellon Exp $ Copyright 1995-2001 Internet Software Consortium.";#endif  static char copyright[] ="Copyright 1995-2001 Internet Software Consortium.";static char arr [] = "All rights reserved.";static char message [] = "Internet Software Consortium DHCP Server";static char url [] = "For info, please visit http://www.isc.org/products/DHCP";#include "dhcpd.h"#include "version.h"#include <omapip/omapip_p.h>static void usage PROTO ((void));TIME cur_time;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)

⌨️ 快捷键说明

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