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

📄 ifconfig.c

📁 一个基于linux的TCP/IP协议栈的实现
💻 C
📖 第 1 页 / 共 2 页
字号:
			goterr |= set_flag(ifr.ifr_name, IFF_PROMISC);			spp++;			continue;		}		if( !strcmp(*spp, "-promisc") ){			goterr |= clr_flag(ifr.ifr_name, IFF_PROMISC);			spp++;			continue;		}		if( !strcmp(*spp, "allmulti") ){			goterr |= set_flag(ifr.ifr_name, IFF_ALLMULTI);			spp++;			continue;		}		if( !strcmp(*spp, "-allmulti") ){			goterr |= clr_flag(ifr.ifr_name, IFF_ALLMULTI);			spp++;			continue;		}		if (!strcmp(*spp, "up")) {			goterr |= set_flag(ifr.ifr_name, (IFF_UP | IFF_RUNNING));			spp++;			continue;		}		if (!strcmp(*spp, "down")) {			goterr |= clr_flag(ifr.ifr_name, IFF_UP);			spp++;			continue;		}		if( !strcmp(*spp, "metric") ){			if( *++spp == NULL )				usage();			ifr.ifr_metric = atoi(*spp);			if( ioctl(skfd, SIOCSIFMETRIC, &ifr) < 0 ){				fprintf(stderr, "SIOCSIFMETRIC: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}		if( !strcmp(*spp, "mtu") ){			if( *++spp == NULL )				usage();			ifr.ifr_mtu = atoi(*spp);			if( ioctl(skfd, SIOCSIFMTU, &ifr) < 0 ){				fprintf(stderr, "SIOCSIFMTU: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}		if( !strcmp(*spp, "-broadcast") ){			goterr |= clr_flag(ifr.ifr_name, IFF_BROADCAST);			spp++;			continue;		}		if( !strcmp(*spp, "broadcast") ){			if( *++spp != NULL ){				safe_strncpy(host, *spp, (sizeof host));				if (ap->input(0, host, &sa) < 0) {					ap->herror(host);					goterr = 1;					spp++;					continue;				}				memcpy((char *) &ifr.ifr_broadaddr, (char *) &sa, sizeof(struct sockaddr));				if (ioctl(ap->fd, SIOCSIFBRDADDR, &ifr) < 0) {					fprintf(stderr, "SIOCSIFBRDADDR: %s\n", strerror(errno));					goterr = 1;				}				spp++;			}			goterr |= set_flag(ifr.ifr_name, IFF_BROADCAST);			continue;		}		if( !strcmp(*spp, "dstaddr") ){			if (*++spp == NULL)				usage();			safe_strncpy( host, *spp, (sizeof host) );			if( ap->input(0, host, &sa) < 0 ){				ap->herror(host);				goterr = 1;				spp++;				continue;			}			memcpy( (char *) &ifr.ifr_dstaddr, (char *) &sa, sizeof(struct sockaddr) );			if( ioctl(ap->fd, SIOCSIFDSTADDR, &ifr) < 0 ){				fprintf(stderr, "SIOCSIFDSTADDR: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}		if( !strcmp(*spp, "netmask") ){			if (*++spp == NULL || didnetmask)				usage();			safe_strncpy(host, *spp, (sizeof host));			if( ap->input(0, host, &sa) < 0 ){				ap->herror(host);				goterr = 1;				spp++;				continue;			}			didnetmask++;			goterr = set_netmask(ap->fd, &ifr, &sa);			spp++;			continue;		}		if( !strcmp(*spp, "txqueuelen") ){			if( *++spp == NULL )				usage();			ifr.ifr_qlen = strtoul(*spp, NULL, 0);			if( ioctl(skfd, SIOCSIFTXQLEN, &ifr) < 0 ){				fprintf(stderr, "SIOCSIFTXQLEN: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}				if( !strcmp(*spp, "mem_start") ){			if (*++spp == NULL)				usage();			if( ioctl(skfd, SIOCGIFMAP, &ifr) < 0 ){				fprintf(stderr, "mem_start: SIOCGIFMAP: %s\n", strerror(errno));				spp++;				goterr = 1;				continue;			}			ifr.ifr_map.mem_start = strtoul(*spp, NULL, 0);			if( ioctl(skfd, SIOCSIFMAP, &ifr) < 0 ){				fprintf(stderr, "mem_start: SIOCSIFMAP: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}		if( !strcmp(*spp, "io_addr") ){			if( *++spp == NULL )				usage();			if( ioctl(skfd, SIOCGIFMAP, &ifr) < 0 ){				fprintf(stderr, "io_addr: SIOCGIFMAP: %s\n", strerror(errno));				spp++;				goterr = 1;				continue;			}			ifr.ifr_map.base_addr = strtol(*spp, NULL, 0);			if( ioctl(skfd, SIOCSIFMAP, &ifr) < 0 ){				fprintf(stderr, "io_addr: SIOCSIFMAP: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}		if( !strcmp(*spp, "irq") ){			if( *++spp == NULL )				usage();			if( ioctl(skfd, SIOCGIFMAP, &ifr) < 0 ){				fprintf(stderr, "irq: SIOCGIFMAP: %s\n", strerror(errno));				goterr = 1;				spp++;				continue;			}			ifr.ifr_map.irq = atoi(*spp);			if( ioctl(skfd, SIOCSIFMAP, &ifr) < 0 ){				fprintf(stderr, "irq: SIOCSIFMAP: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}		if( !strcmp(*spp, "-pointopoint") ){			goterr |= clr_flag( ifr.ifr_name, IFF_POINTOPOINT );			spp++;			continue;		}		if( !strcmp(*spp, "pointopoint") ){			if( *(spp + 1) != NULL ){				spp++;				safe_strncpy(host, *spp, (sizeof host));				if( ap->input(0, host, &sa) ){					ap->herror(host);					goterr = 1;					spp++;					continue;				}				memcpy((char *) &ifr.ifr_dstaddr, (char *) &sa, sizeof(struct sockaddr));				if( ioctl(ap->fd, SIOCSIFDSTADDR, &ifr) < 0 ){					fprintf(stderr, "SIOCSIFDSTADDR: %s\n", strerror(errno));					goterr = 1;				}			}			goterr |= set_flag(ifr.ifr_name, IFF_POINTOPOINT);			spp++;			continue;		};		if( !strcmp(*spp, "hw") ){			if( *++spp == NULL )				usage();			if( (hw = get_hwtype(*spp)) == NULL )				usage();			if( hw->input == NULL ){				fprintf(stderr, "hw address type `%s' has no handler to set address. failed.\n", 								*spp);				spp+=2;				goterr = 1;				continue;			}			if( *++spp == NULL )				usage();			safe_strncpy( host, *spp, (sizeof host) );			if( hw->input(host, &sa) < 0 ){				fprintf(stderr, "%s: invalid %s address.\n", host, hw->name);				goterr = 1;				spp++;				continue;			}			memcpy((char *) &ifr.ifr_hwaddr, (char *) &sa, sizeof(struct sockaddr));			if( ioctl(skfd, SIOCSIFHWADDR, &ifr) < 0 ){				fprintf(stderr, "SIOCSIFHWADDR: %s\n", strerror(errno));				goterr = 1;			}			spp++;			continue;		}		if( !strcmp(*spp, "add") ){			if( *++spp == NULL )				usage();			{				unsigned long ip, nm, bc;				safe_strncpy(host, *spp, (sizeof host));				if( myinet_aftype.input(0, host, (struct sockaddr *)&sin) < 0 ){					ap->herror(host);					goterr = 1;					spp++;					continue;				}				fd = get_socket_for_af(MY_AF_INET);				if( fd < 0 ){					fprintf(stderr, "No support for INET on this system.\n");					goterr = 1;					spp++;					continue;				}				memcpy(&ip, &sin.sin_addr.s_addr, sizeof(unsigned long));				if( get_nmbc_parent(ifr.ifr_name, &nm, &bc) < 0 ){					fprintf( stderr, "Interface %s not initialized\n", ifr.ifr_name );					goterr = 1;					spp++;					continue;				}				set_ifstate(ifr.ifr_name, ip, nm, bc, 1);			}			spp++;			continue;		}		if( !strcmp(*spp, "del") ){			if( *++spp == NULL )				usage();			{				unsigned long ip, nm, bc;				safe_strncpy(host, *spp, (sizeof host));				if( myinet_aftype.input(0, host, (struct sockaddr *)&sin) < 0 ){					ap->herror(host);					goterr = 1;					spp++;					continue;				}				fd = get_socket_for_af( MY_AF_INET );				if( fd < 0 ){					fprintf(stderr, "No support for INET on this system.\n");					goterr = 1;					spp++;					continue;				}				memcpy(&ip, &sin.sin_addr.s_addr, sizeof(unsigned long));				if( get_nmbc_parent(ifr.ifr_name, &nm, &bc) < 0 ){					fprintf(stderr, "Interface %s not initialized\n", ifr.ifr_name);					goterr = 1;					spp++;					continue;				}				set_ifstate(ifr.ifr_name, ip, nm, bc, 0);			}			spp++;			continue;		}		safe_strncpy(host, *spp, (sizeof host));		if( ap->getmask ){			switch( ap->getmask(host, &sa, NULL) ){				case -1:					usage();					break;				case 1:					if( didnetmask )						usage();					goterr = set_netmask(skfd, &ifr, &sa);					didnetmask++;					break;			}		}		if( ap->input == NULL ){			fprintf(stderr, "ifconfig: Cannot set address for this protocol family.\n");			exit(1);		}		if( ap->input(0, host, &sa) < 0 ){			ap->herror(host);			fprintf(stderr, "ifconfig: `--help' gives usage information.\n");			exit(1);		}		memcpy((char *) &ifr.ifr_addr, (char *) &sa, sizeof(struct sockaddr));		{			int r = 0;			switch( ap->af ){				case MY_AF_INET:					fd = get_socket_for_af(MY_AF_INET);					if( fd < 0 ){						fprintf(stderr, "No support for INET on this system.\n");						exit(1);					}					r = ioctl(fd, SIOCSIFADDR, &ifr);					break;				default:					fprintf(stderr, "Don't know how to set addresses for family %d.\n", ap->af);					exit(1);			}			if( r < 0 ){				perror("SIOCSIFADDR");				goterr = 1;			}		}		{			char *ptr;			short int found_colon = 0;			for( ptr = ifr.ifr_name; *ptr; ptr++  )				if (*ptr == ':') found_colon++;			if( !(found_colon && *(ptr - 1) == '-') )				goterr |= set_flag(ifr.ifr_name, (IFF_UP | IFF_RUNNING));		}		spp++;	}	return 0;}

⌨️ 快捷键说明

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