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

📄 startup.c

📁 OLSR Implementation for XORP
💻 C
字号:
/* * Copyright (c) 2004 Ying Ge, Communication Research Center Canada. * * Copyright (c) 2002, 2003 Maoyu Wang, Communication Research Center Canada. * * By Ying Ge: * 1. Change the OLSR packet format and message processing procedure based  *    on the OLSR RFC. * 2. Add support of multiple interfaces to OLSR, including MID message  *    creating and processing procedure as specified in the OLSR RFC. * 3. Add QoS Support to OLSR * * By Maoyu Wang: * 1. Ported OLSR from IPv4 to IPv6. * 2. Added the Host and Network Association (HNA) functionality into OLSR. * 3. Added the default gateway functionality into OLSR by extending the HNA  *    message usage. The default gateway functionality supported the mobility *	  by cooperating with Mobile IPv6 for a mobile node as well as supported  * 	  Internet access for MANET nodes.  * * DISTRIBUTED WITH NO WARRANTY, EXPRESS OR IMPLIED. * See the GNU Library General Public License (file COPYING in the distribution) * for conditions of use and redistribution *//* * This Copyright notice is in French. An English summary is given * but the referee text is the French one. * * Copyright (c) 2000, 2001 Adokoe.Plakoo@inria.fr, INRIA Rocquencourt, *                          Anis.Laouiti@inria.fr, INRIA Rocquencourt. * * Ce logiciel informatique est disponible aux conditions * usuelles dans la recherche, c'est-à-dire qu'il peut * être utilisé, copié, modifié, distribué à l'unique * condition que ce texte soit conservé afin que * l'origine de ce logiciel soit reconnue. * Le nom de l'Institut National de Recherche en Informatique * et en Automatique (INRIA), ou d'une personne morale * ou physique ayant participé à l'élaboration de ce logiciel ne peut * être utilisé sans son accord préalable explicite. *  * Ce logiciel est fourni tel quel sans aucune garantie, * support ou responsabilité d'aucune sorte. * Certaines parties de ce logiciel sont dérivées de sources developpees par * University of California, Berkeley et ses contributeurs couvertes  * par des copyrights. * This software is available with usual "research" terms * with the aim of retain credits of the software.  * Permission to use, copy, modify and distribute this software for any * purpose and without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies, * and the name of INRIA, or any contributor not be used in advertising * or publicity pertaining to this material without the prior explicit * permission. The software is provided "as is" without any * warranties, support or liabilities of any kind. * This product includes software developed by the University of * California, Berkeley and its contributors protected by copyrights. *//* * Copyright (c) 1983, 1988, 1993 *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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. * * From: @(#)startup.c	5.19 (Berkeley) 2/28/91 * From: @(#)startup.c	8.1 (Berkeley) 6/5/93 */char startup_rcsid[] =   "$Id: startup.c,v 1.2 2000/12/06 10:36:12 prima Exp $";/* * Routing Table Management Daemon */#include "defs.h"#include <sys/ioctl.h>#include <net/if.h>#include <errno.h>#include <stdio.h> // fscanf(),fopen#include <arpa/inet.h>struct	interface *ifnet;struct	interface **ifnext = &ifnet;//CRC v4->v6 addolsr_u8_t hw_addr[8];extern int option_i;extern struct interf_name *interf_names;/* list of interface on which olsr				          packets will be send *//* * Find the network broadcast interfaces which have configured. * If several interfaces are configurated, only interfaces * matching with the names specified in option are maintained. * In this version only one interface is required.  */void ifinit(void){	struct interface ifs, *ifp;	struct interf_name *nt;	int skif, nbinterf = 0, interf_num = 0;	char buf[BUFSIZ], *cp, *cplim;	struct ifconf ifc;	struct ifreq ifreq, *ifr;	struct sockaddr_in *sin;	u_long i;	int save, n;	if ((skif = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 	{		syslog(LOG_ERR, "socket: %m");		close(skif);		return;	}	ifc.ifc_len = sizeof (buf);	ifc.ifc_buf = buf;	if (ioctl(skif, SIOCGIFCONF, (char *)&ifc) < 0) 	{		syslog(LOG_ERR, "ioctl (get interface configuration)");		close(skif);		return;	}        	ifr = ifc.ifc_req;	#define size(p) (sizeof (p))	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */        int addLen=sizeof (ifr->ifr_name) + size(ifr->ifr_addr);	printf("What is the length %d %d\n",ifc.ifc_len,addLen);	for (cp = buf; cp < cplim; cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) 	{		ifr = (struct ifreq *)cp;//added by L.Qin, what is in the configure		printf("\n~~~~~~ The interface from configure is %s\n",cp);		bzero((char *)&ifs, sizeof(ifs));		ifs.int_addr = ifr->ifr_addr;		ifreq = *ifr;		if (ioctl(skif, SIOCGIFFLAGS, (char *)&ifreq) < 0) 		{			syslog(LOG_ERR, "%s: ioctl (get interface flags)",			ifr->ifr_name);			continue;		}               		/*		 *  Save only flags we need from the kernel structure.		 */		/* CRC v4->v6 del		save = IFF_UP|IFF_BROADCAST|IFF_LOOPBACK|IFF_POINTOPOINT;*/				// CRC v4->v6 add 		/* IFF_MULTICAST is not here because in ipv6 multicast is enabled in all interface*/		save = IFF_UP|IFF_LOOPBACK|IFF_POINTOPOINT;		ifs.int_flags = (ifreq.ifr_flags & save) | IFF_INTERFACE;		if ((ifs.int_flags & IFF_UP) == 0 || ifr->ifr_addr.sa_family == AF_UNSPEC)			continue;     		printf("after AF_UNSPEC\n");		/* argh, this'll have to change sometime */		if (ifs.int_addr.sa_family != AF_INET)			continue;		printf("after AF_INET\n");		if (ifs.int_flags & IFF_POINTOPOINT)			continue;               printf("after IFF_POINTOPOINT\n");		 /* Assume no duplicate interface */		if (if_ifwithaddr(&ifs.int_addr))			continue;//		printf("after withaddr %s\n",if_ifwithaddr(&ifs.int_addr)->int_name);		/* Don't add loopback interface to ifnet list */		if (ifs.int_flags & IFF_LOOPBACK)			continue;		printf("after IFF_LOOPBACK\n");		/*CRC v4->v6 del		if (ifs.int_flags & IFF_BROADCAST) 		{			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) 			{				syslog(LOG_ERR, "%s: ioctl (get broadaddr)",ifr->ifr_name);				continue;			}			ifs.int_broadaddr = ifreq.ifr_broadaddr;		}*/						//CRC v4->v6 add		memcpy(&(ifs.int6_multi_addr),&multiaddr, sizeof(struct olsr_ip_addr));					#ifdef SIOCGIFMETRIC			if (ioctl(skif, SIOCGIFMETRIC, (char *)&ifreq) < 0) 			{				syslog(LOG_ERR, "%s: ioctl (get metric)",ifr->ifr_name);				ifs.int_metric = 0;			} 			else				ifs.int_metric = ifreq.ifr_metric;		#else			ifs.int_metric = 0;		#endif		/*		 * Use a minimum metric of one;		 * treat the interface metric (default 0)		 * as an increment to the hop count of one.		 */		ifs.int_metric++;				/* CRC v4->v6 del		if (ioctl(skif, SIOCGIFNETMASK, (char *)&ifreq) < 0) 		{			syslog(LOG_ERR, "%s: ioctl (get netmask)",ifr->ifr_name);			continue;		}		sin = (struct sockaddr_in *)&ifreq.ifr_addr;		ifs.int_subnetmask = ntohl(sin->sin_addr.s_addr);		sin = (struct sockaddr_in *)&ifs.int_addr;		i = ntohl(sin->sin_addr.s_addr);		if (IN_CLASSA(i))			ifs.int_netmask = IN_CLASSA_NET;		else if (IN_CLASSB(i))			ifs.int_netmask = IN_CLASSB_NET;		else			ifs.int_netmask = IN_CLASSC_NET;		ifs.int_net = i & ifs.int_netmask;		ifs.int_subnet = i & ifs.int_subnetmask;		if (ifs.int_subnetmask != ifs.int_netmask)			ifs.int_flags |= IFF_SUBNET; */					for (nt = interf_names; nt != NULL; nt = nt->next)		{					printf ("... nt name: %s\n", nt->name);		}				printf ("...........finished\n");		/*		 * Verify the type of interfaces specified by option "-i"		 */		for (nt = interf_names; nt != NULL; nt = nt->next)							if (strcmp(ifr->ifr_name, nt->name) == 0)			{				break;			}			if (nt == NULL && option_i)			{		   		continue;			}		                	nbinterf++;			printf ("nbinterf=%d\n", nbinterf);			ifp = (struct interface *)malloc(sizeof (struct interface));			if (ifp == 0) 			{				printf("olsrd: out of memory\n");				break;			}			*ifp = ifs;			ifp->int_name = (char *)malloc(strlen(ifr->ifr_name) + 1);			if (ifp->int_name == 0) 			{				fprintf(stderr, "olsrd: ifinit: out of memory\n");				syslog(LOG_ERR, "olsrd: ifinit: out of memory\n");				close(skif);				return;			}			strcpy(ifp->int_name, ifr->ifr_name);			ifp->my_index = (nbinterf-1); //added by Y.Ge			printf (".....init name: %s\n", ifp->int_name);					//CRC v4->v6 add			if(ioctl(skif, SIOCGIFHWADDR, (void*)ifr)<0)			{				syslog(LOG_ERR,"%s: ioctl (get hardware address)",ifr->ifr_name);				continue;			}						memcpy(hw_addr, ifr->ifr_hwaddr.sa_data, 3);			memcpy(hw_addr+5, ifr->ifr_hwaddr.sa_data+3, 3);			hw_addr[3] = 0xFF;			hw_addr[4] = 0xFE;			hw_addr[0] ^= 2;				if(debug_level>6)			{				for(n=0; n<8; n++)					printf("hw_addr[%d]= %x    \n", n, hw_addr[n]);			}					memcpy(ifp->int6_hw_addr,hw_addr,8);					//CRC v4->v6 add			if_get_ipv6info(ifp);			if_get_multiaddr(ifp);					*ifnext = ifp;			ifnext = &ifp->int_next;			traceinit(ifp);	}	close(skif);	/*if (nbinterf != 1)	{				printf("olsrd: %d interface(s) are detected.\n", nbinterf);		printf("olsrd: One (only one) interface is required.\n");		printf("       Use option -i to specify one interface.\n");		exit(1);	}*/}/*CRC v4->v6 add  Get ipv6 site address, index of each interface, and ipv6 global addresses (future function)*/void if_get_ipv6info (struct interface* ifptr){    	FILE *f;        char addr6[40], devname[20],str[INET6_ADDRSTRLEN];        struct in6_addr ip6addr;        int plen, scope, dad_status;	    unsigned int  if_idx;        char addr6p[8][5];		if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {	while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",		      addr6p[0], addr6p[1], addr6p[2], addr6p[3],		      addr6p[4], addr6p[5], addr6p[6], addr6p[7],		  &if_idx, &plen, &scope, &dad_status, devname) != EOF) {		  	    if (!strcmp(devname, ifptr->int_name)) {		sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",			addr6p[0], addr6p[1], addr6p[2], addr6p[3],			addr6p[4], addr6p[5], addr6p[6], addr6p[7]);    		inet_pton(AF_INET6,addr6,&ip6addr);		          	    switch (scope) {		case IPV6_ADDR_LINKLOCAL:		    inet_pton(AF_INET6,addr6,&ip6addr);				    memcpy(&(ifptr->int6_link_addr),&ip6addr, sizeof(struct olsr_ip_addr));		    ifptr->int6_link_prefix = plen;		    ifptr->int_ifindex = if_idx;		    break;		case IPV6_ADDR_SITELOCAL:		    inet_pton(AF_INET6,addr6,&ip6addr);				    memcpy(&(ifptr->int6_site_addr),&ip6addr, sizeof(struct olsr_ip_addr));		    ifptr->int6_site_prefix = plen;		    ifptr->int_ifindex = if_idx;		    break;		case IPV6_ADDR_GLOBAL:		    /*do something here in the future*/		    break;		default:		    break;		}	   }	 }	 fclose(f); /*end of "while" */   } /*end of "if"*/      return;}void if_get_multiaddr(struct interface *ifptr){	memcpy(&(ifptr->int6_multi_addr),&multiaddr, sizeof(struct olsr_ip_addr));}void ifchoice(char *name){  struct interf_name *interf_n;       interf_n = (struct interf_name *)malloc(sizeof(interf_n));       if (interf_n == 0) {	     fprintf(stderr, "olsrd: ifinit: out of memory\n");	     syslog(LOG_ERR, "olsrd: ifinit: out of memory\n");	     return;       }       interf_n->name = (char *)malloc(strlen(name));       if (interf_n->name == 0) {	     fprintf(stderr, "olsrd: ifinit: out of memory\n");	     syslog(LOG_ERR, "olsrd: ifinit: out of memory\n");	     return;       }       strcpy(interf_n->name, name);       interf_n->next = interf_names;       interf_names = interf_n;}

⌨️ 快捷键说明

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