netsetup.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 1,405 行 · 第 1/3 页

C
1,405
字号
				max = 13;			else				max = 5;			printf("\n");			for (;;) {				buf[0] = '\0';				prompt(buf,512,"How many bits of the host number will be used for subnets? ");				if (buf[0] == '\0')					continue;				else					subbits = atoi(buf);				if (subbits < 1 || subbits >= max) {					printf("It must be a number between 1 & %d\n",max-1);					continue;				}				if (yesno(YES,"\nYou are using %d bits of the host number for subnets.\nIs this correct", subbits))					break;			}			netmask.s_addr = 0xffffffffL;			netmask.s_addr <<= ((long)max + 3L - subbits);		} else {			subbits = 0;			if (IN_CLASSA(ntohl(addr.s_addr)))				netmask.s_addr = 0xff000000L;			else if (IN_CLASSB(ntohl(addr.s_addr)))				netmask.s_addr = 0xffff0000L;			else				netmask.s_addr = 0xffffff00L;		}/* Include top subbits of machine number when using subnets */#define SUBMASK (subbits ? (0xffffffL<<(max+3-subbits)&machnum) : 0)		netmask.s_addr = htonl(netmask.s_addr);		taddr1 = inet_makeaddr(netnum, ~ntohl(netmask.s_addr) | SUBMASK);		taddr2 = inet_makeaddr(netnum, 0L | SUBMASK);prmsg="\n\An Internet Protocol (IP) broadcast address is an address in which the\n\bits of the host number are either all ones or all zeros.  You need to\n\know what the other hosts on your existing local area network are using as\n\their broadcast address.  If you are setting up your own network, use\n\the default..\n\\n\WARNING! You must use the same broadcast address as all of the other\n\hosts on your network.  This is very important!\n";				printf(prmsg);		sprintf(buf, "%s", inet_ntoa(taddr1));		sprintf(buf2, "%s", inet_ntoa(taddr2));prmsg="\n\If you use all ones your IP broadcast address is: %s\n\if you use all zeros your IP broadcast address is: %s\n\\n\Enter a \"1\" if you use all ones, or \"0\" if you use all zeros [1]: ";		do {			char resp[20];			prompt(resp, 20, prmsg, buf, buf2);			if (resp[0] == '\0')				oneorzero = 1;			else				oneorzero = atoi(resp);			if (oneorzero != 1 && oneorzero != 0) {				fprintf(stderr,"\n\You must enter \"1\" or \"0\"\n");				continue;			}			if (oneorzero == 1) 				broadaddr = taddr1;			else				broadaddr = taddr2;		} while (!yesno(YES,"\nYou are using all %s for your broadcast address.\nIs this correct", (oneorzero ? "ones" : "zeros")));prmsg="\n\Please specify the device name and unit number of your network\n\interface.  This information is entered in the %s file so\n\that the correct device is initialized when you bring the system to\n\multiuser mode.  The device name consists of the network device\n\as found in your system config file.  The following are some common\n\network device names:\n\\n\        Device Name     Description\n\        -----------     -----------\n\          xna0          DEBNI - BI bus\n\          xna4          DEMNA - XMI bus\n\          ni0           DEBNT, DEBNA - BI bus\n\          de0           DEUNA, DELUA - UNIBUS\n\          qe0           DEQNA, DELQA - Q-Bus\n\          ln0           DESVA, Busless LANCE network interface\n\	  ne0		Second Generation Ethernet Controller interface\n\	  fza0		DEFZA, TURBOchannel FDDI network interface\n\          scs0          SCS network interface to CI\n\\n";		printf(prmsg, RC);		/*		 * Determine default network interface.		 */		if_default = get_interface(); 		for(;;) {askagain:							if (if_default)				prompt(buf, 512, "What is the device name of your Network Interface [%s]? ", if_default);			else				prompt(buf, 512,"What is the device name of your Network Interface? ");			if (buf[0] == '\0')				if (if_default)					strcpy(buf, if_default);				else					goto askagain;							cp = buf;			while (*cp == ' ' || *cp == '\t')				cp++;			xp = cp;			while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')				cp++;			*cp = '\0';			if (yesno(YES, "\nYour Network interface is \"%s\".  Is this correct", xp))				break;		}prmsg="\n\The routed program can be invoked at boot time to manage the network\n\routing tables.  Normally it is only run on systems with multiple\n\interfaces.\n";		printf(prmsg);		do {			i=(yesno(NO,"\nDo you want \/etc\/routed started each time %s is booted",hostname));			if (i == NO) {				i=yesno(YES,"Routed will not be started.  Is that correct");				routed=FALSE;			} else {				i=yesno(YES,"Routed will be started.  Is that correct");				routed=TRUE;			}		} while (i==NO);		printf("\n***** UPDATING %s WITH network configuration information *****\n", RC);		/*		 * Update the /etc/rc.local file, WITH networking info.		 */		fp = edit(RC);		if (fp == NULL) {			fprintf(stderr, "%s is not set up for a network.\n", RC);		} else {			fprintf(fp, "/ifconfig.*`\\/bin\\/hostname`.*$/c\n");			fprintf(fp, "/etc/ifconfig %s `/bin/hostname` ", xp);			fprintf(fp, "broadcast %s ", inet_ntoa(broadaddr));			fprintf(fp, "netmask %s\n", inet_ntoa(netmask));			fprintf(fp, ".\n");			if (routed) {				fprintf(fp, "/routed/\n");				fprintf(fp, ".,+2s/^#//\n");			}			fprintf(fp, "w\n");			fprintf(fp, "q\n");			fflush(fp);			pclose(fp);		}		/*		 * Set up /etc/networks here.  Supply a default network		 * name and alias. If user enters a zero length string		 * (<RETURN> only) use default.		 */		/*		 * The network number that goes into /etc/networks is		 * the logical OR of the real internet network number		 * PLUS the subnet part (if any).  So /etc/networks		 * actually contains names and numbers of subnets.  We		 * derive the number by taking the broadcast address		 * (which contains the subnet part) and masking off just		 * the network-subnet piece.		 */#define	UC(b)	(((int)b)&0xff)		if (IN_CLASSA(ntohl(addr.s_addr)))			mask = IN_CLASSA_NSHIFT - subbits;		else if (IN_CLASSB(ntohl(addr.s_addr)))			mask = IN_CLASSB_NSHIFT - subbits;		else			mask = IN_CLASSC_NSHIFT;		taddr1.s_addr = broadaddr.s_addr & netmask.s_addr;		if (mask >= 24) {			register char *p;			p = (char *)&taddr1;			sprintf(buf, "%d", UC(p[0]));		} else if (mask >= 16) {			register char *p;			p = (char *)&taddr1;			sprintf(buf, "%d.%d", UC(p[0]), UC(p[1]));		} else if (mask >= 8) {			register char *p;			p = (char *)&taddr1;			sprintf(buf, "%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]));		} else if (mask >= 0) {			register char *p;			p = (char *)&taddr1;			sprintf(buf, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));		}prmsg="\n\Network numbers have names so you can refer to them by name rather than\n\by number.  The network name is entered in the /etc/networks file.  If\n\you are adding %s to an existing network, you should use the same\n\names as the other hosts on the network use for the various network\n\numbers.  If you are not adding %s to an existing network, you can\n\name the network number whatever you want.  You can assign alias names\n\for the network number.\n\\n";		printf(prmsg,hostname,hostname);		do {			if (NULL == prompt(netname, 128,			"What is the name of network %s [ethernet]? ", buf))				strcpy(netname, "ethernet");			i = yesno(YES, "\nThe network name for %s is \"%s\".  Is this correct",							hostname, netname);		} while ( i == NO);		if (!netname[0])			strcpy(netname, "ethernet");		printf("\n");		do {			if (prompt(netalias, 128,			    "Enter any aliases for %s:  ", netname))			{				i = yesno(YES,				    "\nThe alias%s for %s %s %s.\nIs this correct",						plural(netalias) ? "es" : "",						netname,						plural(netalias) ? "are" : "is",						quote(netalias));			} else {				i = yesno(YES,				    "You do not have any aliases for %s.  Is this correct", netname);			}		} while (i == NO);		printf("\n***** UPDATING %s WITH ", NETWORKS);		printf("%s *****\n", netname);		/* check for duplicate entry in /etc/networks and remove it */		if ((np = getnetbyname(netname)) != NULL) {			/* remove duplicate entry before adding same */		       fprintf(stderr,"Removing duplicate entry, %s\n",netname);			fp = edit(NETWORKS);			if (fp == NULL) {fprintf(stderr, "cannot remove duplicate entry (%s) \nfrom %s file.\n", netname, NETWORKS);			} else {				fprintf(fp, "/^%s/d\n",netname);				fprintf(fp, "w\n");				fprintf(fp, "q\n");				fflush(fp);				pclose(fp);			}		}		/* add the netname to /etc/networks */		fp = fopen(NETWORKS, "a");		if (fp == NULL) {			fprintf(stderr, "%s is not set up.\n", NETWORKS);		} else {			fprintf(fp, "%s %s%s%s\n", netname,					buf,  /* buf is the network number figured above */					netalias[0]? " " : "", netalias);			/* add "loop" only if not there */			if ((np = getnetbyname("loop")) == NULL)				fprintf(fp, "loop 127 loopback\n");			fflush(fp);			fclose(fp);		}	}	/*	 * If user is just adding host names to the network, or at	 * install time user selected to put the host on a network,	 * (didn't exit above) then add host names to network.	 * common to: first time setup and adding host names later.	 */	printf("\n***** ADD/CHANGE SYSTEMS IN %s *****\n", HOSTS);prmsg="\n\Enter the host name, abbreviations, network number, and host number for\n\each host on the network.  Enter this information on separate lines\n\when prompted.  This information is stored in the %s file.  When\n\finished, press the RETURN key at the hostname prompt.\n";	printf(prmsg, HOSTS);	fp = edit(HOSTS);	if (fp == NULL) {		fprintf(stderr, "%s is not set up.\n", HOSTS);	} else for (;;) {		printf("\n");		if (prompt(machname, 128,		    "Enter the name of the host you want to add to %s: ", HOSTS) == NULL) {			if (yesno(YES,"Are you finished adding hosts"))				break;			else				continue;		}		if (!yesno(YES, "\nYou want to add host \"%s\".  Is this correct", machname))			continue;		hp = gethostbyname(machname);		if (hp != NULL) {			printf("\n%s already exists with address %s\n",			      machname, inet_ntoa(*(long *)hp->h_addr));			if (!yesno(NO, "Do you want to replace it"))				continue;			fprintf(fp, "g/%s[ 	]%s/d\n",				inet_ntoa(*(long *)hp->h_addr), machname);			fprintf(fp, "w\n");			fflush(fp);		}		do {			if (NULL == prompt(machabbrev, 128,			    "\nEnter any abbreviations for \"%s\", separated by a blank space.\nIf you do not want any abbreviation names, press the RETURN key: ", machname))			{				i = yesno(YES,					"\nYou do not want any abbreviation names for \"%s\".  Is this correct",								machname);			} else {				i = yesno(YES,				  "\nThe abbreviation%s for %s %s %s.\nIs this correct",						plural(machabbrev) ? "s":"",						machname,						plural(machabbrev) ? "are":"is",						quote(machabbrev));			}		} while (i == NO);		netnum = getnet(machname);		machnum = getmach(machname, netnum);		addr = inet_makeaddr(netnum, machnum);		hp = gethostbyaddr(&addr, sizeof(addr), AF_INET);		if (hp != NULL) {			printf("%s is already assigned to %s\n",				inet_ntoa(addr), hp->h_name);			if (!yesno(NO, "Remove %s from %s", hp->h_name, HOSTS))				continue;			fprintf(fp, "g/%s[ 	]%s/d\n",				inet_ntoa(addr), hp->h_name);			fprintf(fp, "w\n");			fflush(fp);		}		fprintf(fp, "$a\n");		fprintf(fp, "%s %s %s\n", inet_ntoa(addr),					machname, machabbrev);		fprintf(fp, ".\n");		fprintf(fp, "w\n");		fflush(fp);	}	if (fp) {		fprintf(fp, "w\n");		fprintf(fp, "q\n");		fflush(fp);		pclose(fp);	}	/*	 * Set up /etc/hosts.equiv with trusted hosts here.	 */	if ((fp = edit(HOSTS_EQUIV)) == NULL) {		fprintf(stderr, "%s is not set up\n", HOSTS_EQUIV);	} else {prmsg="\n\Enter the names of trusted hosts.  Trusted hosts are systems you\n\consider to be secure.  Be careful if you select trusted hosts.  Any\n\users on a trusted host can log in to %s without password\n\verification if they have a valid account on your system.  The names of\n\the trusted hosts are stored in the %s file.  When you\n\have finished entering the names of trusted hosts, press the RETURN key.\n\";		printf(prmsg, hostname, HOSTS_EQUIV);		for (;;) {			if (prompt(trustname, 128,				   "\nEnter the name of a trusted host: ") == NULL)			{				if (yesno(YES,				    "Have you finished entering the names of trusted hosts"))					break;				else					continue;			}			if ((hp = gethostbyname(trustname)) == NULL) {			    printf("\n\"%s\" is not in %s.\n", trustname, HOSTS);			    printf("\"%s\" is not added to %s\n", trustname,								HOSTS_EQUIV);			} else {				if (!yesno(YES,				    "\n\"%s\" is a trusted host.  Is this correct",								trustname))					continue;				fprintf(fp, "g/^%s$/d\n", trustname);				fprintf(fp, "a\n%s\n", trustname);				fprintf(fp, ".\n");				fprintf(fp, "w\n");				fflush(fp);			}		}		fprintf(fp, "w\n");		fprintf(fp, "q\n");		fflush(fp);		pclose(fp);	}	/*	 * Set up /usr/hosts/*	 */	printf("\n***** SETTING UP %s DIRECTORY *****\n", UHOSTS);	if (access(UHOSTS, 0) < 0) {		mkdir(UHOSTS, 0755);		chown(UHOSTS, 3, 3);	}	sethostent(1);	while ((hp = gethostent()) != NULL) {		if (strcmp(hp->h_name, "localhost") == 0)			continue;		sprintf(buf, "%s/%s", UHOSTS, hp->h_name);		symlink("/usr/ucb/rsh", buf);	}	endhostent();	/* Setup /usr/spool/rwho directory, OK if fails */	mkdir(RWHODIR, 0755);	chown(RWHODIR, 3, 3);	leave(0);}/* * exit the program with status */leave(status)int status;{	if (status == 0)		printf("\n***** NETWORK SETUP COMPLETE *****\n\n");	exit(status);}char *prompt(buf, len, str, a1, a2, a3, a4, a5)char *str, *buf;int len;int a1, a2, a3, a4, a5;{	char mybuf[512];	printf(str, a1, a2, a3, a4, a5);	fflush(stdout);	if (gets(mybuf) == NULL) {		eof();		return(NULL);	}	strncpy(buf, mybuf, len);	buf[len-1] = '\0';	if (buf[0] == '\0')		return(NULL);	return(buf);}

⌨️ 快捷键说明

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