commands.c

来自「Linux下的socks的程序源程序,用于socks的代理服务」· C语言 代码 · 共 2,732 行 · 第 1/5 页

C
2,732
字号
	if (net < 0) {	    perror("telnet: socket");	    return 0;	}#if	defined(IP_OPTIONS) && defined(IPPROTO_IP)	if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)		perror("setsockopt (IP_OPTIONS)");#endif#if	defined(IPPROTO_IP) && defined(IP_TOS)	{# if	defined(HAS_GETTOS)	    struct tosent *tp;	    if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))		tos = tp->t_tos;# endif	    if (tos < 0)		tos = 020;	/* Low Delay bit */	    if (tos		&& (setsockopt(net, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)		&& (errno != ENOPROTOOPT))		    perror("telnet: setsockopt (IP_TOS) (ignored)");	}#endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */	if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {		perror("setsockopt (SO_DEBUG)");	}	if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {#if	defined(h_addr)		/* In 4.3, this is a #define */	    if (host && host->h_addr_list[1]) {		int oerrno = errno;		fprintf(stderr, "telnet: connect to address %s: ",						inet_ntoa(sin.sin_addr));		errno = oerrno;		perror((char *)0);		host->h_addr_list++;		memcpy((caddr_t)&sin.sin_addr, 			host->h_addr_list[0], host->h_length);		(void) NetClose(net);		continue;	    }#endif	/* defined(h_addr) */	    perror("telnet: Unable to connect to remote host");	    return 0;	}	connected++;#if	defined(AUTHENTICATE)	auth_encrypt_connect(connected);#endif    } while (connected == 0);    cmdrc(hostp, hostname);    if (autologin && user == NULL) {	struct passwd *pw;	user = getenv("USER");	if (user == NULL ||	    (pw = getpwnam(user)) && pw->pw_uid != getuid()) {		if (pw = getpwuid(getuid()))			user = pw->pw_name;		else			user = NULL;	}    }    if (user) {	env_define((unsigned char *)"USER", (unsigned char *)user);	env_export((unsigned char *)"USER");    }    (void) call(status, "status", "notmuch", 0);    if (setjmp(peerdied) == 0)	telnet(user);    (void) NetClose(net);    ExitString("Connection closed by foreign host.\n",1);    /*NOTREACHED*/}#define HELPINDENT (sizeof ("connect"))static char	openhelp[] =	"connect to a site",	closehelp[] =	"close current connection",	logouthelp[] =	"forcibly logout remote user and close the connection",	quithelp[] =	"exit telnet",	statushelp[] =	"print status information",	helphelp[] =	"print help information",	sendhelp[] =	"transmit special characters ('send ?' for more)",	sethelp[] = 	"set operating parameters ('set ?' for more)",	unsethelp[] = 	"unset operating parameters ('unset ?' for more)",	togglestring[] ="toggle operating parameters ('toggle ?' for more)",	slchelp[] =	"change state of special charaters ('slc ?' for more)",	displayhelp[] =	"display operating parameters",#if	defined(TN3270) && defined(unix)	transcomhelp[] = "specify Unix command for transparent mode pipe",#endif	/* defined(TN3270) && defined(unix) */#if	defined(AUTHENTICATE)	authhelp[] =	"turn on (off) authentication ('auth ?' for more)",#endif#if	defined(unix)	zhelp[] =	"suspend telnet",#endif	/* defined(unix) */	shellhelp[] =	"invoke a subshell",	envhelp[] =	"change environment variables ('environ ?' for more)",	modestring[] = "try to enter line or character mode ('mode ?' for more)";extern int	help();static Command cmdtab[] = {	{ "close",	closehelp,	bye,		1 },	{ "logout",	logouthelp,	logout,		1 },	{ "display",	displayhelp,	display,	0 },	{ "mode",	modestring,	modecmd,	0 },	{ "open",	openhelp,	tn,		0 },	{ "quit",	quithelp,	quit,		0 },	{ "send",	sendhelp,	sendcmd,	0 },	{ "set",	sethelp,	setcmd,		0 },	{ "unset",	unsethelp,	unsetcmd,	0 },	{ "status",	statushelp,	status,		0 },	{ "toggle",	togglestring,	toggle,		0 },	{ "slc",	slchelp,	slccmd,		0 },#if	defined(TN3270) && defined(unix)	{ "transcom",	transcomhelp,	settranscom,	0 },#endif	/* defined(TN3270) && defined(unix) */#if	defined(AUTHENTICATE)	{ "auth",	authhelp,	auth_cmd,	0 },#endif#if	defined(ENCRYPT)	{ "encrypt",	encrypthelp,	encrypt_cmd,	0 },#endif#if	defined(unix)	{ "z",		zhelp,		suspend,	0 },#endif	/* defined(unix) */#if	defined(TN3270)	{ "!",		shellhelp,	shell,		1 },#else	{ "!",		shellhelp,	shell,		0 },#endif	{ "environ",	envhelp,	env_cmd,	0 },	{ "?",		helphelp,	help,		0 },	0};static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";static Command cmdtab2[] = {	{ "help",	0,		help,		0 },	{ "escape",	escapehelp,	setescape,	0 },	{ "crmod",	crmodhelp,	togcrmod,	0 },	0};/* * Call routine with argc, argv set from args (terminated by 0). */    /*VARARGS1*/    staticcall(va_alist)    va_dcl{    va_list ap;    typedef int (*intrtn_t)();    intrtn_t routine;    char *args[100];    int argno = 0;    va_start(ap);    routine = (va_arg(ap, intrtn_t));    while ((args[argno++] = va_arg(ap, char *)) != 0) {	;    }    va_end(ap);    return (*routine)(argno-1, args);}    static Command *getcmd(name)    char *name;{    Command *cm;    if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))	return cm;    return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));}    voidcommand(top, tbuf, cnt)    int top;    char *tbuf;    int cnt;{    register Command *c;    setcommandmode();    if (!top) {	putchar('\n');#if	defined(unix)    } else {	(void) signal(SIGINT, SIG_DFL);	(void) signal(SIGQUIT, SIG_DFL);#endif	/* defined(unix) */    }    for (;;) {	if (rlogin == _POSIX_VDISABLE)		printf("%s> ", prompt);	if (tbuf) {	    register char *cp;	    cp = line;	    while (cnt > 0 && (*cp++ = *tbuf++) != '\n')		cnt--;	    tbuf = 0;	    if (cp == line || *--cp != '\n' || cp == line)		goto getline;	    *cp = '\0';	    if (rlogin == _POSIX_VDISABLE)	        printf("%s\n", line);	} else {	getline:	    if (rlogin != _POSIX_VDISABLE)		printf("%s> ", prompt);	    if (fgets(line, sizeof(line), stdin) == NULL) {		if (feof(stdin) || ferror(stdin)) {		    (void) quit();		    /*NOTREACHED*/		}		break;	    }	}	if (line[0] == 0)	    break;	makeargv();	if (margv[0] == 0) {	    break;	}	c = getcmd(margv[0]);	if (Ambiguous(c)) {	    printf("?Ambiguous command\n");	    continue;	}	if (c == 0) {	    printf("?Invalid command\n");	    continue;	}	if (c->needconnect && !connected) {	    printf("?Need to be connected first.\n");	    continue;	}	if ((*c->handler)(margc, margv)) {	    break;	}    }    if (!top) {	if (!connected) {	    longjmp(toplevel, 1);	    /*NOTREACHED*/	}#if	defined(TN3270)	if (shell_active == 0) {	    setconnmode(0);	}#else	/* defined(TN3270) */	setconnmode(0);#endif	/* defined(TN3270) */    }}/* * Help command. */	statichelp(argc, argv)	int argc;	char *argv[];{	register Command *c;	if (argc == 1) {		printf("Commands may be abbreviated.  Commands are:\n\n");		for (c = cmdtab; c->name; c++)			if (c->help) {				printf("%-*s\t%s\n", HELPINDENT, c->name,								    c->help);			}		return 0;	}	while (--argc > 0) {		register char *arg;		arg = *++argv;		c = getcmd(arg);		if (Ambiguous(c))			printf("?Ambiguous help command %s\n", arg);		else if (c == (Command *)0)			printf("?Invalid help command %s\n", arg);		else			printf("%s\n", c->help);	}	return 0;}static char *rcname = 0;static char rcbuf[128];cmdrc(m1, m2)	char *m1, *m2;{    register Command *c;    FILE *rcfile;    int gotmachine = 0;    int l1 = strlen(m1);    int l2 = strlen(m2);    char m1save[64];    if (skiprc)	return;    strcpy(m1save, m1);    m1 = m1save;    if (rcname == 0) {	rcname = getenv("HOME");	if (rcname)	    strcpy(rcbuf, rcname);	else	    rcbuf[0] = '\0';	strcat(rcbuf, "/.telnetrc");	rcname = rcbuf;    }    if ((rcfile = fopen(rcname, "r")) == 0) {	return;    }    for (;;) {	if (fgets(line, sizeof(line), rcfile) == NULL)	    break;	if (line[0] == 0)	    break;	if (line[0] == '#')	    continue;	if (gotmachine) {	    if (!isspace(line[0]))		gotmachine = 0;	}	if (gotmachine == 0) {	    if (isspace(line[0]))		continue;	    if (strncasecmp(line, m1, l1) == 0)		strncpy(line, &line[l1], sizeof(line) - l1);	    else if (strncasecmp(line, m2, l2) == 0)		strncpy(line, &line[l2], sizeof(line) - l2);	    else if (strncasecmp(line, "DEFAULT", 7) == 0)		strncpy(line, &line[7], sizeof(line) - 7);	    else		continue;	    if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')		continue;	    gotmachine = 1;	}	makeargv();	if (margv[0] == 0)	    continue;	c = getcmd(margv[0]);	if (Ambiguous(c)) {	    printf("?Ambiguous command: %s\n", margv[0]);	    continue;	}	if (c == 0) {	    printf("?Invalid command: %s\n", margv[0]);	    continue;	}	/*	 * This should never happen...	 */	if (c->needconnect && !connected) {	    printf("?Need to be connected first for %s.\n", margv[0]);	    continue;	}	(*c->handler)(margc, margv);    }    fclose(rcfile);}#if	defined(IP_OPTIONS) && defined(IPPROTO_IP)/* * Source route is handed in as *	[!]@hop1@hop2...[@|:]dst * If the leading ! is present, it is a * strict source route, otherwise it is * assmed to be a loose source route. * * We fill in the source route option as *	hop1,hop2,hop3...dest * and return a pointer to hop1, which will * be the address to connect() to. * * Arguments: *	arg:	pointer to route list to decipher * *	cpp: 	If *cpp is not equal to NULL, this is a *		pointer to a pointer to a character array *		that should be filled in with the option. * *	lenp:	pointer to an integer that contains the *		length of *cpp if *cpp != NULL. * * Return values: * *	Returns the address of the host to connect to.  If the *	return value is -1, there was a syntax error in the *	option, either unknown characters, or too many hosts. *	If the return value is 0, one of the hostnames in the *	path is unknown, and *cpp is set to point to the bad *	hostname. * *	*cpp:	If *cpp was equal to NULL, it will be filled *		in with a pointer to our static area that has *		the option filled in.  This will be 32bit aligned. *  *	*lenp:	This will be filled in with how long the option *		pointed to by *cpp is. *	 */	u_int32sourceroute(arg, cpp, lenp)	char	*arg;	char	**cpp;	int	*lenp;{	static char lsr[44];	char *cp, *cp2, *lsrp, *lsrep;	register int tmp;	struct in_addr sin_addr;	register struct hostent *host = 0;	register char c;	/*	 * Verify the arguments, and make sure we have	 * at least 7 bytes for the option.	 */	if (cpp == NULL || lenp == NULL)		/* return((u_int32)-1); */		return(INADDR_NONE /* H.Naef, nexos ag */);	if (*cpp != NULL && *lenp < 7)		/* return((u_int32)-1); */		return(INADDR_NONE /* H.Naef, nexos ag */);	/*	 * Decide whether we have a buffer passed to us,	 * or if we need to use our own static buffer.	 */	if (*cpp) {		lsrp = *cpp;		lsrep = lsrp + *lenp;	} else {		*cpp = lsrp = lsr;		lsrep = lsrp + 44;	}	cp = arg;	/*	 * Next, decide whether we have a loose source	 * route or a strict source route, and fill in	 * the begining of the option.	 */	if (*cp == '!') {		cp++;		*lsrp++ = IPOPT_SSRR;	} else		*lsrp++ = IPOPT_LSRR;	if (*cp != '@')		/* return((u_int32)-1); */		return(INADDR_NONE /* H.Naef, nexos ag */);	lsrp++;		/* skip over length, we'll fill it in later */	*lsrp++ = 4;	cp++;	sin_addr.s_addr = 0;	for (c = 0;;) {		if (c == ':')			cp2 = 0;		else for (cp2 = cp; c = *cp2; cp2++) {			if (c == ',') {				*cp2++ = '\0';				if (*cp2 == '@')					cp2++;			} else if (c == '@') {				*cp2++ = '\0';			} else if (c == ':') {				*cp2++ = '\0';			} else				continue;			break;		}		if (!c)			cp2 = 0;		/* if ((tmp = inet_addr(cp)) != -1) { */		if ((tmp = inet_addr(cp)) != INADDR_NONE /* H.Naef, nexos ag */) {			sin_addr.s_addr = tmp;		} else if (host = gethostbyname(cp)) {#if	defined(h_addr)			memcpy((caddr_t)&sin_addr,				host->h_addr_list[0], host->h_length);#else			memcpy((caddr_t)&sin_addr, host->h_addr, host->h_length);#endif		} else {			*cpp = cp;			return(0);		}		memcpy(lsrp, (char *)&sin_addr, 4);		lsrp += 4;		if (cp2)			cp = cp2;		else			break;		/*		 * Check to make sure there is space for next address		 */		if (lsrp + 4 > lsrep)			/* return((u_int32)-1); */			return(INADDR_NONE /* H.Naef, nexos ag */);	}	if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {		*cpp = 0;		*lenp = 0;		/* return((u_int32)-1); */		return(INADDR_NONE /* H.Naef, nexos ag */);	}	*lsrp++ = IPOPT_NOP; /* 32 bit word align it */	*lenp = lsrp - *cpp;	return(sin_addr.s_addr);}#endif

⌨️ 快捷键说明

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