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

📄 ifenslave.c

📁 《嵌入式系统设计与实例开发实验教材二源码》Linux内核移植与编译实验
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Mode: C; * ifenslave.c: Configure network interfaces for parallel routing. * *	This program controls the Linux implementation of running multiple *	network interfaces in parallel. * * Usage:	ifenslave [-v] master-interface < slave-interface [metric <N>] > ... * * Author:	Donald Becker <becker@cesdis.gsfc.nasa.gov> *		Copyright 1994-1996 Donald Becker * *		This program is free software; you can redistribute it *		and/or modify it under the terms of the GNU General Public *		License as published by the Free Software Foundation. * *	The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O *	Center of Excellence in Space Data and Information Sciences *	   Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771 * *  Changes : *    - 2000/10/02 Willy Tarreau <willy at meta-x.org> : *       - few fixes. Master's MAC address is now correctly taken from *         the first device when not previously set ; *       - detach support : call BOND_RELEASE to detach an enslaved interface. *       - give a mini-howto from command-line help : # ifenslave -h * *    - 2001/02/16 Chad N. Tindel <ctindel at ieee dot org> : *       - Master is now brought down before setting the MAC address.  In *         the 2.4 kernel you can't change the MAC address while the device is *         up because you get EBUSY.   * *    - 2001/09/13 Takao Indoh <indou dot takao at jp dot fujitsu dot com> *       - Added the ability to change the active interface on a mode 1 bond *         at runtime. * *    - 2001/10/23 Chad N. Tindel <ctindel at ieee dot org> : *       - No longer set the MAC address of the master.  The bond device will *         take care of this itself *       - Try the SIOC*** versions of the bonding ioctls before using the *         old versions */static char *version ="ifenslave.c:v0.07 9/9/97  Donald Becker (becker@cesdis.gsfc.nasa.gov).\n""detach support added on 2000/10/02 by Willy Tarreau (willy at meta-x.org).\n""2.4 kernel support added on 2001/02/16 by Chad N. Tindel (ctindel at ieee dot org.\n";static const char *usage_msg ="Usage: ifenslave [-adfrvVh] <master-interface> < <slave-if> [metric <N>] > ...\n""       ifenslave -c master-interface slave-if\n";static const char *howto_msg ="Usage: ifenslave [-adfrvVh] <master-interface> < <slave-if> [metric <N>] > ...\n""       ifenslave -c master-interface slave-if\n""\n""       To create a bond device, simply follow these three steps :\n""       - ensure that the required drivers are properly loaded :\n""         # modprobe bonding ; modprobe <3c59x|eepro100|pcnet32|tulip|...>\n""       - assign an IP address to the bond device :\n""         # ifconfig bond0 <addr> netmask <mask> broadcast <bcast>\n""       - attach all the interfaces you need to the bond device :\n""         # ifenslave bond0 eth0 eth1 eth2\n""         If bond0 didn't have a MAC address, it will take eth0's. Then, all\n""         interfaces attached AFTER this assignment will get the same MAC addr.\n""\n""       To detach a dead interface without setting the bond device down :\n""         # ifenslave -d bond0 eth1\n""\n""       To set the bond device down and automatically release all the slaves :\n""         # ifconfig bond0 down\n""\n""       To change active slave :\n""         # ifenslave -c bond0 eth0\n""\n";#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include <string.h>#include <errno.h>#include <fcntl.h>#include <getopt.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <linux/if.h>#include <linux/if_arp.h>#include <linux/if_ether.h>#include <linux/if_bonding.h>#include <linux/sockios.h>struct option longopts[] = { /* { name  has_arg  *flag  val } */    {"all-interfaces", 0, 0, 'a'},	/* Show all interfaces. */    {"force",       0, 0, 'f'},		/* Force the operation. */    {"help", 		0, 0, '?'},		/* Give help */	{"howto",       0, 0, 'h'},     /* Give some more help */    {"receive-slave", 0, 0, 'r'},	/* Make a receive-only slave.  */    {"verbose", 	0, 0, 'v'},		/* Report each action taken.  */    {"version", 	0, 0, 'V'},		/* Emit version information.  */    {"detach",       0, 0, 'd'},	/* Detach a slave interface. */    {"change-active", 0, 0, 'c'},	/* Change the active slave.  */    { 0, 0, 0, 0 }};/* Command-line flags. */unsigned intopt_a = 0,					/* Show-all-interfaces flag. */opt_f = 0,					/* Force the operation. */opt_r = 0,					/* Set up a Rx-only slave. */opt_d = 0,					/* detach a slave interface. */opt_c = 0,					/* change-active-slave flag. */verbose = 0,					/* Verbose flag. */opt_version = 0,opt_howto = 0;int skfd = -1;					/* AF_INET socket for ioctl() calls.	*/static void if_print(char *ifname);intmain(int argc, char **argv){	struct ifreq  ifr2, if_hwaddr, if_ipaddr, if_metric, if_mtu, if_dstaddr;	struct ifreq  if_netmask, if_brdaddr, if_flags;	int goterr = 0;	int c, errflag = 0;	sa_family_t master_family;	char **spp, *master_ifname, *slave_ifname;	int hwaddr_notset;	while ((c = getopt_long(argc, argv, "acdfrvV?h", longopts, 0)) != EOF)		switch (c) {		case 'a': opt_a++; break;		case 'f': opt_f++; break;		case 'r': opt_r++; break;		case 'd': opt_d++; break;		case 'c': opt_c++; break;		case 'v': verbose++;		break;		case 'V': opt_version++;	break;		case 'h': opt_howto++;	break;		case '?': errflag++;		}	/* option check */	if (opt_c)		if(opt_a || opt_f || opt_r || opt_d || verbose || opt_version ||		   opt_howto || errflag ) {			fprintf(stderr, usage_msg);			return 2;		}	if (errflag) {		fprintf(stderr, usage_msg);		return 2;	}	if (opt_howto) {		fprintf(stderr, howto_msg);		return 0;	}	if (verbose || opt_version) {		printf(version);		if (opt_version)			exit(0);	}	/* Open a basic socket. */	if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) {		perror("socket");		exit(-1);	}	if (verbose)		fprintf(stderr, "DEBUG: argc=%d, optind=%d and argv[optind] is %s.\n",				argc, optind, argv[optind]);	/* No remaining args means show all interfaces. */	if (optind == argc) {		if_print((char *)NULL);		(void) close(skfd);		exit(0);	}	/* Copy the interface name. */	spp = argv + optind;	master_ifname = *spp++;	slave_ifname = *spp++;	/* Check command line. */	if (opt_c) {		char **tempp = spp;		if ((master_ifname == NULL)||(slave_ifname == NULL)||(*tempp++ != NULL)) {			fprintf(stderr, usage_msg);			return 2;		}	}	/* A single args means show the configuration for this interface. */	if (slave_ifname == NULL) {		if_print(master_ifname);		(void) close(skfd);		exit(0);	}	/* Get the vitals from the master interface. */	{		struct ifreq *ifra[7] = { &if_ipaddr, &if_mtu, &if_dstaddr,								  &if_brdaddr, &if_netmask, &if_flags,								  &if_hwaddr };		const char *req_name[7] = {			"IP address", "MTU", "destination address",			"broadcast address", "netmask", "status flags",			"hardware address" };		const int ioctl_req_type[7] = {			SIOCGIFADDR, SIOCGIFMTU, SIOCGIFDSTADDR,			SIOCGIFBRDADDR, SIOCGIFNETMASK, SIOCGIFFLAGS,			SIOCGIFHWADDR };		int i;		for (i = 0; i < 7; i++) {			strncpy(ifra[i]->ifr_name, master_ifname, IFNAMSIZ);			if (ioctl(skfd, ioctl_req_type[i], ifra[i]) < 0) {				fprintf(stderr,						"Something broke getting the master's %s: %s.\n",						req_name[i], strerror(errno));			}		}		hwaddr_notset = 1; /* assume master's address not set yet */		for (i = 0; hwaddr_notset && (i < 6); i++) {			hwaddr_notset &= ((unsigned char *)if_hwaddr.ifr_hwaddr.sa_data)[i] == 0;		}		/* The family '1' is ARPHRD_ETHER for ethernet. */		if (if_hwaddr.ifr_hwaddr.sa_family != 1 && !opt_f) {			fprintf(stderr, "The specified master interface '%s' is not"					" ethernet-like.\n  This program is designed to work"					" with ethernet-like network interfaces.\n"					" Use the '-f' option to force the operation.\n",					master_ifname);			exit (1);		}		master_family = if_hwaddr.ifr_hwaddr.sa_family;		if (verbose) {			unsigned char *hwaddr = (unsigned char *)if_hwaddr.ifr_hwaddr.sa_data;			printf("The current hardware address (SIOCGIFHWADDR) of %s is type %d  "				   "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n", master_ifname,				   if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],				   hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);		}	}	/* do this when enslaving interfaces */	do {		if (opt_d) {  /* detach a slave interface from the master */			strncpy(if_flags.ifr_name, master_ifname, IFNAMSIZ);			strncpy(if_flags.ifr_slave, slave_ifname, IFNAMSIZ);			if ((ioctl(skfd, SIOCBONDRELEASE, &if_flags) < 0) &&				(ioctl(skfd, BOND_RELEASE_OLD, &if_flags) < 0)) {					fprintf(stderr,	"SIOCBONDRELEASE: cannot detach %s from %s. errno=%s.\n",							slave_ifname, master_ifname, strerror(errno));			}			else {  /* we'll set the interface down to avoid any conflicts due to					   same IP/MAC */				strncpy(ifr2.ifr_name, slave_ifname, IFNAMSIZ);				if (ioctl(skfd, SIOCGIFFLAGS, &ifr2) < 0) {					int saved_errno = errno;					fprintf(stderr, "SIOCGIFFLAGS on %s failed: %s\n", slave_ifname,							strerror(saved_errno));				}				else {					ifr2.ifr_flags &= ~(IFF_UP | IFF_RUNNING);					if (ioctl(skfd, SIOCSIFFLAGS, &ifr2) < 0) {						int saved_errno = errno;						fprintf(stderr, "Shutting down interface %s failed: %s\n",								slave_ifname, strerror(saved_errno));					}				}

⌨️ 快捷键说明

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