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

📄 discover.c

📁 DHCP服务器源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* dispatch.c   Network input dispatcher... *//* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") * Copyright (c) 1995-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 copyright[] ="$Id: discover.c,v 1.42.2.15 2004/06/10 17:59:16 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium.  All rights reserved.\n";#endif /* not lint */#include "dhcpd.h"#include <sys/ioctl.h>struct interface_info *interfaces, *dummy_interfaces, *fallback_interface;int interfaces_invalidated;int quiet_interface_discovery;u_int16_t local_port;u_int16_t remote_port;int (*dhcp_interface_setup_hook) (struct interface_info *, struct iaddr *);int (*dhcp_interface_discovery_hook) (struct interface_info *);isc_result_t (*dhcp_interface_startup_hook) (struct interface_info *);int (*dhcp_interface_shutdown_hook) (struct interface_info *);struct in_addr limited_broadcast;struct in_addr local_address;void (*bootp_packet_handler) PROTO ((struct interface_info *,				     struct dhcp_packet *, unsigned,				     unsigned int,				     struct iaddr, struct hardware *));omapi_object_type_t *dhcp_type_interface;#if defined (TRACING)trace_type_t *interface_trace;trace_type_t *inpacket_trace;trace_type_t *outpacket_trace;#endifstruct interface_info **interface_vector;int interface_count;int interface_max;OMAPI_OBJECT_ALLOC (interface, struct interface_info, dhcp_type_interface)isc_result_t interface_setup (){	isc_result_t status;	status = omapi_object_type_register (&dhcp_type_interface,					     "interface",					     dhcp_interface_set_value,					     dhcp_interface_get_value,					     dhcp_interface_destroy,					     dhcp_interface_signal_handler,					     dhcp_interface_stuff_values,					     dhcp_interface_lookup, 					     dhcp_interface_create,					     dhcp_interface_remove,					     0, 0, 0,					     sizeof (struct interface_info),					     interface_initialize, RC_MISC);	if (status != ISC_R_SUCCESS)		log_fatal ("Can't register interface object type: %s",			   isc_result_totext (status));	return status;}#if defined (TRACING)void interface_trace_setup (){	interface_trace = trace_type_register ("interface", (void *)0,					       trace_interface_input,					       trace_interface_stop, MDL);	inpacket_trace = trace_type_register ("inpacket", (void *)0,					       trace_inpacket_input,					       trace_inpacket_stop, MDL);	outpacket_trace = trace_type_register ("outpacket", (void *)0,					       trace_outpacket_input,					       trace_outpacket_stop, MDL);}#endifisc_result_t interface_initialize (omapi_object_t *ipo,				   const char *file, int line){	struct interface_info *ip = (struct interface_info *)ipo;	ip -> rfdesc = ip -> wfdesc = -1;	return ISC_R_SUCCESS;}/* Use the SIOCGIFCONF ioctl to get a list of all the attached interfaces.   For each interface that's of type INET and not the loopback interface,   register that interface with the network I/O software, figure out what   subnet it's on, and add it to the list of interfaces. */void discover_interfaces (state)	int state;{	struct interface_info *tmp, *ip;	struct interface_info *last, *next;	char buf [2048];	struct ifconf ic;	struct ifreq ifr;	int i;	int sock;	int address_count = 0;	struct subnet *subnet;	struct shared_network *share;	struct sockaddr_in foo;	int ir;	struct ifreq *tif;#ifdef ALIAS_NAMES_PERMUTED	char *s;#endif	isc_result_t status;	static int setup_fallback = 0;	int wifcount = 0;	/* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */	if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)		log_fatal ("Can't create addrlist socket");	/* Get the interface configuration information... */#ifdef SIOCGIFCONF_ZERO_PROBE	/* linux will only tell us how long a buffer it wants if we give it	 * a null buffer first. So, do a dry run to figure out the length.	 * 	 * XXX this code is duplicated from below because trying to fold	 * the logic into the if statement and goto resulted in excesssive	 * obfuscation. The intent is that unless you run Linux you shouldn't	 * have to deal with this. */	ic.ifc_len = 0;	ic.ifc_ifcu.ifcu_buf = (caddr_t)NULL;#else	/* otherwise, we just feed it a starting size, and it'll tell us if	 * it needs more */	ic.ifc_len = sizeof buf;	ic.ifc_ifcu.ifcu_buf = (caddr_t)buf;#endif      gifconf_again:	i = ioctl(sock, SIOCGIFCONF, &ic);	if (i < 0)		log_fatal ("ioctl: SIOCGIFCONF: %m");#ifdef SIOCGIFCONF_ZERO_PROBE	/* Workaround for SIOCGIFCONF bug on some Linux versions. */	if (ic.ifc_ifcu.ifcu_buf == 0 && ic.ifc_len == 0) {		ic.ifc_len = sizeof buf;		ic.ifc_ifcu.ifcu_buf = (caddr_t)buf;		goto gifconf_again;	}#endif	/* If the SIOCGIFCONF resulted in more data than would fit in	   a buffer, allocate a bigger buffer. */	if ((ic.ifc_ifcu.ifcu_buf == buf #ifdef SIOCGIFCONF_ZERO_PROBE	     || ic.ifc_ifcu.ifcu_buf == 0#endif		) && ic.ifc_len > sizeof buf) {		ic.ifc_ifcu.ifcu_buf = dmalloc ((size_t)ic.ifc_len, MDL);		if (!ic.ifc_ifcu.ifcu_buf)			log_fatal ("Can't allocate SIOCGIFCONF buffer.");		goto gifconf_again;#ifdef SIOCGIFCONF_ZERO_PROBE	} else if (ic.ifc_ifcu.ifcu_buf == 0) {		ic.ifc_ifcu.ifcu_buf = (caddr_t)buf;		ic.ifc_len = sizeof buf;		goto gifconf_again;#endif	}			/* If we already have a list of interfaces, and we're running as	   a DHCP server, the interfaces were requested. */	if (interfaces && (state == DISCOVER_SERVER ||			   state == DISCOVER_RELAY ||			   state == DISCOVER_REQUESTED))		ir = 0;	else if (state == DISCOVER_UNCONFIGURED)		ir = INTERFACE_REQUESTED | INTERFACE_AUTOMATIC;	else		ir = INTERFACE_REQUESTED;	/* Cycle through the list of interfaces looking for IP addresses. */	for (i = 0; i < ic.ifc_len;) {		struct ifreq *ifp = (struct ifreq *)((caddr_t)ic.ifc_req + i);#ifdef HAVE_SA_LEN		if (ifp -> ifr_addr.sa_len > sizeof (struct sockaddr))			i += (sizeof ifp -> ifr_name) + ifp -> ifr_addr.sa_len;		else#endif			i += sizeof *ifp;#ifdef ALIAS_NAMES_PERMUTED		if ((s = strrchr (ifp -> ifr_name, ':'))) {			*s = 0;		}#endif#ifdef SKIP_DUMMY_INTERFACES		if (!strncmp (ifp -> ifr_name, "dummy", 5))			continue;#endif		/* See if this is the sort of interface we want to		   deal with. */		strcpy (ifr.ifr_name, ifp -> ifr_name);		if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0)			log_fatal ("Can't get interface flags for %s: %m",			       ifr.ifr_name);		/* See if we've seen an interface that matches this one. */		for (tmp = interfaces; tmp; tmp = tmp -> next)			if (!strcmp (tmp -> name, ifp -> ifr_name))				break;		/* Skip non broadcast interfaces (plus loopback and		   point-to-point in case an OS incorrectly marks them		   as broadcast). Also skip down interfaces unless we're		   trying to get a list of configurable interfaces. */		if (((!(ifr.ifr_flags & IFF_BROADCAST) ||		      ifr.ifr_flags & IFF_LOOPBACK ||		      ifr.ifr_flags & IFF_POINTOPOINT) && !tmp) ||		    (!(ifr.ifr_flags & IFF_UP) &&		     state != DISCOVER_UNCONFIGURED))			continue;				/* If there isn't already an interface by this name,		   allocate one. */		if (!tmp) {			tmp = (struct interface_info *)0;			status = interface_allocate (&tmp, MDL);			if (status != ISC_R_SUCCESS)				log_fatal ("Error allocating interface %s: %s",					   ifp -> ifr_name,					   isc_result_totext (status));			strcpy (tmp -> name, ifp -> ifr_name);			interface_snorf (tmp, ir);			interface_dereference (&tmp, MDL);			tmp = interfaces; /* XXX */		}		if (dhcp_interface_discovery_hook)			(*dhcp_interface_discovery_hook) (tmp);		/* If we have the capability, extract link information		   and record it in a linked list. */#ifdef HAVE_AF_LINK		if (ifp -> ifr_addr.sa_family == AF_LINK) {			struct sockaddr_dl *foo = ((struct sockaddr_dl *)						   (&ifp -> ifr_addr));#if defined (HAVE_SIN_LEN)			tmp -> hw_address.hlen = foo -> sdl_alen;#else			tmp -> hw_address.hlen = 6; /* XXX!!! */#endif			tmp -> hw_address.hbuf [0] = HTYPE_ETHER; /* XXX */			memcpy (&tmp -> hw_address.hbuf [1],				LLADDR (foo), tmp -> hw_address.hlen);			tmp -> hw_address.hlen++;	/* for type. */		} else#endif /* AF_LINK */		if (ifp -> ifr_addr.sa_family == AF_INET) {			struct iaddr addr;			/* Get a pointer to the address... */			memcpy (&foo, &ifp -> ifr_addr,				sizeof ifp -> ifr_addr);			/* We don't want the loopback interface. */			if (foo.sin_addr.s_addr == htonl (INADDR_LOOPBACK) &&			    ((tmp -> flags & INTERFACE_AUTOMATIC) &&			     state == DISCOVER_SERVER))			    continue;			/* If this is the first real IP address we've			   found, keep a pointer to ifreq structure in			   which we found it. */			if (!tmp -> ifp) {#ifdef HAVE_SA_LEN				unsigned len = ((sizeof ifp -> ifr_name) +						ifp -> ifr_addr.sa_len);#else				unsigned len = sizeof *ifp;#endif				tif = (struct ifreq *)dmalloc (len, MDL);				if (!tif)					log_fatal ("no space for ifp.");				memcpy (tif, ifp, len);				tmp -> ifp = tif;				tmp -> primary_address = foo.sin_addr;			}			/* Grab the address... */			addr.len = 4;			memcpy (addr.iabuf, &foo.sin_addr.s_addr,				addr.len);			if (dhcp_interface_setup_hook)				(*dhcp_interface_setup_hook) (tmp, &addr);		}	}	/* If we allocated a buffer, free it. */	if (ic.ifc_ifcu.ifcu_buf != buf)		dfree (ic.ifc_ifcu.ifcu_buf, MDL);#if defined (LINUX_SLASHPROC_DISCOVERY)	/* On Linux, interfaces that don't have IP addresses don't	   show up in the SIOCGIFCONF syscall.  This only matters for	   the DHCP client, of course - the relay agent and server	   should only care about interfaces that are configured with	   IP addresses anyway.	   The PROCDEV_DEVICE (/proc/net/dev) is a kernel-supplied file	   that, when read, prints a human readable network status.   We	   extract the names of the network devices by skipping the first	   two lines (which are header) and then parsing off everything	   up to the colon in each subsequent line - these lines start	   with the interface name, then a colon, then a bunch of	   statistics. */	if (state == DISCOVER_UNCONFIGURED) {		FILE *proc_dev;		char buffer [256];		int skip = 2;		proc_dev = fopen (PROCDEV_DEVICE, "r");		if (!proc_dev)			log_fatal ("%s: %m", PROCDEV_DEVICE);		while (fgets (buffer, sizeof buffer, proc_dev)) {			char *name = buffer;			char *sep;			/* Skip the first two blocks, which are header			   lines. */			if (skip) {				--skip;				continue;			}			sep = strrchr (buffer, ':');			if (sep)

⌨️ 快捷键说明

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