netsetup.c

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

C
1,405
字号
yesno(t, s, a1, a2, a3, a4, a5)char *s;int t;int a1, a2, a3, a4, a5;{	char buf[512];	for (;;) {		printf(s, a1, a2, a3, a4, a5);		switch(t) {		case 0:			printf(" [no]? ");			break;		case 1:			printf(" [yes]? ");			break;		default:			printf(" (yes or no)? ");			break;		}		fflush(stdout);		buf[0] = '\0';		if (gets(buf) == NULL) {			eof();			return(t);		}		if (buf[0] == 'y' || buf[0] == 'Y')			return(1);		if (buf[0] == 'n' || buf[0] == 'N')			return(0);		if ((t != 2) && (buf[0] == '\0' || buf[0] == '\n'))			return(t);	}	/* To supress the silly warning about statement not reached, */	/* we comment out the following:  */	/* return(0); */}#define	MIN_A	0#define	MAX_A	127#define	MIN_B	(128L*256L)#define	MAX_B	(191L*256L+255L)#define	MIN_C	(192L*256L*256L)#define	MAX_C	(223L*256L*256L+255L*256L+255L)longgetnet(host)char *host;{	struct in_addr addr, inet_num();	long net, inet_lnaof(), inet_netof();	char buf[512];	static int msg = 0;	char *cp;	for(;;) {	    if (msg) {		printf ("\nEnter the network number for %s: ", host);		fflush(stdout);		if (gets(buf) == NULL) {			eof();			printf("\n");			continue;		}		addr = inet_num(buf, 0);		net = inet_netof(addr);		cp = (char *)&addr.s_addr;		if (addr.s_addr == 0L || inet_lnaof(addr) != 0L)			addr.s_addr = -1L;		else if (net > MIN_A && net < MAX_A)			sprintf(buf, "%d", cp[0]&0xff);		else if (net > MIN_B && net < MAX_B)			sprintf(buf, "%d.%d", cp[0]&0xff, cp[1]&0xff);		else if (net > MIN_C && net < MAX_C)			sprintf(buf, "%d.%d.%d", cp[0]&0xff,					cp[1]&0xff, cp[2]&0xff);		else			addr.s_addr = -1L;		if (addr.s_addr != -1L) {			if (!yesno(YES,"\nThe network number is %s.  Is this correct", buf))				continue;			return(net);		} else {			fprintf(stderr,"\n%s is an invalid network number.\n",buf);		}	    }		msg = 1;prmsg="\n\For a Class A network, the network number is in the range 1 through\n\126.  For a Class B network, the network number consists of two fields\n\separated by periods.  The first field is in the range 128 through 191,\n\and the second field is in the range 1 through 254.  For a Class C\n\network, the network number consists of three fields separated by\n\periods.  The first field is in the range 192 through 223, the second\n\field is in the range 0 through 255, and the third field is in the\n\range 1 through 254:\n\\n\   Class A:  1       through 126\n\   Class B:  128.1   through 191.254\n\   Class C:  192.0.1 through 223.255.254\n\\n";		printf(prmsg);	}}struct in_addrinet_num(cp, lhost)	register char *cp;	int lhost;{	long val, base;	register int i, n;	register char c;	long parts[4], *pp = parts;again:	/*	 * Collect number up to ``.''.	 * Values are specified as for C:	 * 0x=hex, 0=octal, other=decimal.	 */	val = 0; base = 10;	if (*cp == '0')		base = 8, cp++;	if (*cp == 'x' || *cp == 'X')		base = 16, cp++;	while (c = *cp) {		if (isdigit(c)) {			val = (val * base) + (c - '0');			cp++;			continue;		}		if (base == 16 && isxdigit(c)) {			val = (val << 4L) + (c + 10 - (islower(c) ? 'a' : 'A'));			cp++;			continue;		}		break;	}	if (*cp == '.') {		/*		 * Internet format:		 *	a.b.c.d		 *	a.b.c	(with c treated as 16-bits)		 *	a.b	(with b treated as 24 bits)		 */		if (pp >= parts + 4) {			val = -1;			return (*(struct in_addr *)&val);		}		*pp++ = val, cp++;		goto again;	}	/*	 * Check for trailing characters.	 */	if (*cp && !isspace(*cp)) {		val = -1;		return (*(struct in_addr *)&val);	}	*pp++ = val;	n = pp - parts;	if (n > 4) {		val = -1;		return(*(struct in_addr *)&val);	}	val = 0;	if (n == 1 && lhost)		val = htonl(parts[0]);	else {		for (i = 0; i < n; i++) {			if (parts[i] > 255 || parts[i] < 0) {				val = -1;				return(*(struct in_addr *)&val);			}			val = (val << 8L) + parts[i];		}		if (lhost == 0)			for (; i < 4; i++)				val <<= 8L;		val = htonl(val);	}	return (*(struct in_addr *)&val);}longgetmach(n, net)char *n;long net;{	struct in_addr addr, addr2, inet_num();	long lna, inet_lnaof();	char buf[512];	static msg = 0;	char *cp;	addr2 = inet_makeaddr(net, 0L);	for(;;) {	    if (msg) {		printf("\nEnter the host number ");		if (subbits)			printf("(including the subnet number) ");		printf("for %s: ", n);		fflush(stdout);		if (gets(buf) == NULL) {			eof();			printf("\n");			continue;		}		addr = inet_num(buf, 1);		if (addr.s_addr != -1L && addr.s_addr != 0L) {			lna = inet_lnaof(addr);/*@			cp = &addr.s_addr;	JSD *//*@			printf("addr = %d.%d.%d.%d\n", cp[0]&0xff, cp[1]&0xff, cp[2]&0xff, cp[3]&0xff);		JSD */			if (lna && ((IN_CLASSA(ntohl(addr2.s_addr)) &&						(lna < 256L*256L*256L - 1)) ||				    (IN_CLASSB(ntohl(addr2.s_addr)) &&						(lna < 256L*256L - 1)) ||				    (lna < 256 - 1)) )			{				cp = (char *)&addr.s_addr;				if (IN_CLASSA(ntohl(addr2.s_addr)))					sprintf(buf, "%d.%d.%d", cp[1]&0xff,							cp[2]&0xff, cp[3]&0xff);				else if (IN_CLASSB(ntohl(addr2.s_addr)))					sprintf(buf, "%d.%d", cp[2]&0xff,								cp[3]&0xff);				else					sprintf(buf, "%d", cp[3]&0xff);				if (!yesno(YES,"\nThe Host number is %s.  Is this correct",buf))					continue;				return(lna);			}		}	    }	    msg = 1;	    if (net < 128) {		printf("You are setting up %s on a Class A network.\n", hostname);		printf("\nValid Class A host numbers ");		if (subbits)			printf("(including the subnet number)\n");prmsg="\consist of three fields separated by\n\periods.  The first two fields are in the range 0 through 255, and\n\the third field is in the range 1 through 254:\n\\n\   0.0.1 through 255.255.254\n\\n\An alternate representation for the three bytes of information specifying\n\the host number is one field in the range 1 through 16777214:\n\\n\   1 through 16777214\n\\n";					printf(prmsg);			    } else if (net < 65536L) {		printf("You are setting up %s on a Class B network.\n", hostname);		printf("\nValid Class B host numbers ");		if (subbits)			printf("(including the subnet number)\n");prmsg="\consist of two fields separated by a\n\period.  The first field is in the range 0 through 255, and the second\n\field is in the range 1 through 254:\n\\n\   0.1 through 255.254\n\\n\An alternate representation for the two bytes of information specifying\n\the host number is one field in the range 1 through 65534:\n\\n\   1 through 65534\n\\n";		printf(prmsg);	    } else {		printf("You are setting up %s on a Class C network.\n", hostname);		printf("Valid Class C host numbers (including the subnet number) are in the range 1 through 254.\n");	    }	}}FILE*edit(file)char *file;{	FILE	*fp;	char	buf[64];	/*	 * close stdout, popen the file for editing, restore	 * stdout, and return the stream pointer.	 */	close(1);	sprintf(buf, "/bin/ed - %s", file);	fp = popen(buf, "w");	dup2(3, 1);	if (fp == NULL)		fprintf(stderr, "Cannot open %s for editing.\n", file);	return(fp);}/* quote() -  make string like: abc def ghi, become: "abc", "def", "ghi". */#include <ctype.h>#define iswhite(s) isspace(s)char *quote(s) 	char *s; {	int state, oldstate, white;	static char buff[256];	char *s2 = buff;	state = oldstate = white = iswhite(' ');  /* prime it */	while (*s) {		if ((state = iswhite(*s)) != oldstate) {			*s2++ = '\"';			if (state == white)				*s2++ = ',';		}		*s2++ = *s++;		oldstate = state;	}	/* if last char was not a space put ending quote*/	if (state == 0)       		*s2++ = '\"';	*s2 = '\0';	/* strip off last comma */	while (--s2 > buff) 			if (!iswhite(*s2)) {			if (*s2 == ',')				*s2 = ' ';			break;		}	return(buff);}/*  *		e o f * * Unexpected EOF, try to recover. */eof() {	if (isatty(0) && (freopen(ttyname(0),"r",stdin) != NULL)) {		printf("\n");		return(0);	}	fprintf(stderr,"\nUnexpected EOF.  Exiting.\n");	leave(1);}/* * *		g e t _ i n t e r f a c e * * Determine the network interface. Look in * the conf file for a possible choice.  Choices are * listed in (char *if_table[]). * */char *get_interface(){	FILE *cf;	char config[512 + 10];	register char *cp;	register int i;	char *ni = 0;	sprintf(config, "%s/%s", CONFDIR, hostname);	cp = config + sizeof(CONFDIR);	upcase(cp);	cf = fopen(config, "r");	if (cf == NULL)		return(0);	/*	 * Build list of hardware devices as found in config file.	 */	parse_config(cf);	/*	 * Find match.	 */	for (i=0; cp=if_table[i]; i++)	{		register int len = strlen(cp);		register struct device *d = (struct device *)device.dq_forw;		while (d != (struct device *)&device)		{			if (strncmp(d->dv_name, cp, len) == 0)			{				ni = d->dv_name;				break;			}			d = (struct device *)d->dv_queue.dq_forw;		}		if (ni)			break;	}	return (ni);}parse_config(cf)FILE *cf;{	char buf[256];	char type[256];	char name[256];	struct device *d;	while (fgets(buf, 255, cf) != NULL)	{		if (sscanf(buf, " %s %s ", type, name) == 2)		{			if (strcmp(type, "device") != 0)				continue;			if (strlen(name) > MAXDEVNAMLEN)				continue;			/*			 * Link it in.			 */			d = (struct device *)emalloc(sizeof(struct device));			strcpy(d->dv_name, name);			insque(d, device.dq_back);		}	}	/***************	for (d = device.dq_forw; d != &device ; d = d->dv_queue.dq_forw)		printf("qelem: %s\n", d->dv_name);	****************/}upcase(cp)char *cp;{	while(*cp)	{		if (islower(*cp))			*cp -= 'a' - 'A';		cp++ ;	}}char *emalloc(size)unsigned long size;{	char *p;	p = (char *)malloc(size);	if (p == 0)	{		fprintf(stderr, "Out of memory.\n");		exit(2);	}	return(p);}

⌨️ 快捷键说明

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