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

📄 commands.cc

📁 里面包含了telnet的客户端和服务器端
💻 CC
📖 第 1 页 / 共 4 页
字号:
	    continue;	}	if (portp == 0) {	    portp = *argv++;	    --argc;	    continue;	}    usage:	printf("usage: %s [-l user] [-a] host-name [port]\n", cmd);	return 0;    }    if (hostp == 0)	goto usage;#if defined(IP_OPTIONS) && defined(HAS_IPPROTO_IP)    if (hostp[0] == '@' || hostp[0] == '!') {	if ((hostname = strrchr(hostp, ':')) == NULL)	    hostname = strrchr(hostp, '@');	hostname++;	srp = 0;	int temp = sourceroute(hostp, &srp, &srlen);	if (temp == 0) {	    herror(srp);	    return 0;	} else if (temp == -1) {	    printf("Bad source route option: %s\n", hostp);	    return 0;	} else {	    sn.sin_addr.s_addr = temp;	    sn.sin_family = AF_INET;	}    }     else {#endif	if (inet_aton(hostp, &sn.sin_addr)) {	    sn.sin_family = AF_INET;	    strcpy(_hostname, hostp);	    hostname = _hostname;	} 	else {	    host = gethostbyname(hostp);	    if (host) {		sn.sin_family = host->h_addrtype;		if (host->h_length > (int)sizeof(sn.sin_addr)) {		    host->h_length = sizeof(sn.sin_addr);		}#if	defined(h_addr)		/* In 4.3, this is a #define */		memcpy((caddr_t)&sn.sin_addr,				host->h_addr_list[0], host->h_length);#else	/* defined(h_addr) */		memcpy((caddr_t)&sn.sin_addr, host->h_addr, host->h_length);#endif	/* defined(h_addr) */		strncpy(_hostname, host->h_name, sizeof(_hostname));		_hostname[sizeof(_hostname)-1] = '\0';		hostname = _hostname;	    } else {		herror(hostp);		return 0;	    }	}#if defined(IP_OPTIONS) && defined(HAS_IPPROTO_IP)    }#endif    if (portp) {	if (*portp == '-') {	    portp++;	    telnetport = 1;	} else	    telnetport = 0;	sn.sin_port = atoi(portp);	if (sn.sin_port == 0) {	    sp = getservbyname(portp, "tcp");	    if (sp)		sn.sin_port = sp->s_port;	    else {		printf("%s: bad port number\n", portp);		return 0;	    }	} 	else {	    sn.sin_port = htons(sn.sin_port);	}    }     else {	if (sp == 0) {	    sp = getservbyname("telnet", "tcp");	    if (sp == 0) {		fprintf(stderr, "telnet: tcp/telnet: unknown service\n");		return 0;	    }	    sn.sin_port = sp->s_port;	}	telnetport = 1;    }    printf("Trying %s...\n", inet_ntoa(sn.sin_addr));    do {	int x = nlink.connect(debug, host, &sn, srp, srlen, tos);	if (!x) return 0;	else if (x==1) continue;	connected++;    } while (connected == 0);    cmdrc(hostp, hostname);    if (autologin && user == NULL) {	struct passwd *pw;	user = getenv("USER");	if (user == NULL ||	    ((pw = getpwnam(user))!=NULL && pw->pw_uid != getuid())) {		if ((pw = getpwuid(getuid()))!=NULL)			user = pw->pw_name;		else			user = NULL;	}    }    if (user) {	env_define("USER", user);	env_export("USER");    }    dostatus(1);    if (sigsetjmp(peerdied, 1) == 0)	telnet(user);    nlink.close(0);    ExitString("Connection closed by foreign host.\n",1);    /*NOTREACHED*/    return 0;}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",	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)",	displayhelp[] =	"display operating parameters",#ifdef TN3270	transcomhelp[] = "specify Unix command for transparent mode pipe",#endif /* TN3270 */	zhelp[] =	"suspend telnet",/*	shellhelp[] =	"invoke a subshell", */	envhelp[] =	"change environment variables ('environ ?' for more)",	modestring[] = "try to enter line or character mode ('mode ?' for more)";static char	crmodhelp[] =	"deprecated command -- use 'toggle crmod' instead";static char	escapehelp[] =	"deprecated command -- use 'set escape' instead";static int help(command_table *, int, const char **);static int doquit(void) {    quit();    return 0;}static int slc_mode_import_0(void) {    slc_mode_import(0);    return 1;}static int slc_mode_import_1(void) {    slc_mode_import(1);    return 1;}static int do_slc_mode_export(void) {    slc_mode_export();    return 1;}static ptrarray<command_entry> cmdtab;static ptrarray<command_entry> cmdtab2;static ptrarray<command_entry> slctab;#define BIND(a,b,c) cmdtab.add(new command_entry(a,b,c))#define BIND2(a,b,c) cmdtab2.add(new command_entry(a,b,c))#define BINDS(a,b,c) slctab.add(new command_entry(a,b,c))void cmdtab_init(void) {    BIND("close",   closehelp,    bye);    BIND("logout",  logouthelp,   logout);    BIND("display", displayhelp,  display);    BIND("mode",    modestring,   modecmd);    BIND("open",    openhelp,     tn);    BIND("quit",    quithelp,     doquit);    BIND("send",    sendhelp,     sendcmd);    BIND("set",     sethelp,      setcmd);    BIND("unset",   unsethelp,    unsetcmd);    BIND("status",  statushelp,   status);    BIND("toggle",  togglestring, toggle);    BIND("slc", "set treatment of special characters\n", &slctab);#ifdef TN3270    BIND("transcom", transcomhelp, settranscom);#endif /* TN3270 */    // BIND("auth", authhelp, auth_cmd);    // BIND("encrypt", encrypthelp, encrypt_cmd);    BIND("z", zhelp, suspend);#if defined(TN3270)   /* why?! */    BIND("!", shellhelp, shell);#endif    BIND("environ", envhelp, env_cmd);    BINDS("export", "Use local special character definitions",	 do_slc_mode_export);    BINDS("import", "Use remote special character definitions",	 slc_mode_import_1);    BINDS("check", "Verify remote special character definitions",	 slc_mode_import_0);    BIND2("escape", escapehelp, setescape);    BIND2("crmod", crmodhelp, togcrmod);}static command_entry *getcmd(command_table *tab, const char *name) {    if (!strcasecmp(name, "?") || 	!strcasecmp(name, "h") ||	!strcasecmp(name, "help")) return (command_entry *)HELP;    command_entry *found = NULL;    for (int i=0; i<tab->num(); i++) {	command_entry *c = (*tab)[i];	if (!strcasecmp(c->getname(), name)) return c;	if (!strncasecmp(c->getname(), name, strlen(name))) {	    if (found) return (command_entry *)AMBIGUOUS;	    found = c;	}    }    if (tab==&cmdtab && !found) return getcmd(&cmdtab2, name);    return found;}static int process_command(command_table *tab, int argc, const char **argv) {    command_entry *c;    c = getcmd(tab, argv[0]);    if (c == HELP) {	help(tab, argc, argv);    }    else if (c == AMBIGUOUS) {	printf("?Ambiguous command\n");    }    else if (c == NULL) {	printf("?Invalid command\n");    }    else {	if (c->call(argc, argv)) return 1;    }    return 0;}void command(int top, const char *tbuf, int cnt) {    setcommandmode();    if (!top) {	putchar('\n');    }     else {	signal(SIGINT, SIG_DFL);	signal(SIGQUIT, SIG_DFL);    }    for (;;) {	if (rlogin == _POSIX_VDISABLE)		printf("%s> ", prompt);	if (tbuf) {	    char *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)) {		    quit();		    /*NOTREACHED*/		}		break;	    }	}	if (line[0] == 0)	    break;	makeargv();	if (margv[0] == 0) {	    break;	}	if (process_command(&cmdtab, margc, margv)) break;    }    if (!top) {	if (!connected) {	    siglongjmp(toplevel, 1);	    /*NOTREACHED*/	}#if	defined(TN3270)	if (shell_active == 0) {	    setconnmode(0);	}#else	/* defined(TN3270) */	setconnmode(0);#endif	/* defined(TN3270) */    }}/* * Help command. */static int help(command_table *tab, int argc, const char *argv[]) {    int i;        if (argc == 1) {	printf("Commands may be abbreviated.  Commands are:\n\n");	for (i = 0; i<tab->num(); i++) (*tab)[i]->describe();	return 0;    }    for (i=1; i<argc; i++) {	command_entry *c = getcmd(tab, argv[i]);	if (c == HELP) {	    printf("Print help information\n");	}	else if (c == AMBIGUOUS) {	    printf("?Ambiguous help command %s\n", argv[i]);	}	else if (c == NULL) {	    printf("?Invalid help command %s\n", argv[i]);	}	else {	    c->gethelp();	}    }    return 0;}static char *rcname = 0;static char rcbuf[128];void cmdrc(const char *m1, const char *m2) {    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;    }    rcfile = fopen(rcname, "r");    if (!rcfile) return;    while (fgets(line, sizeof(line), rcfile)) {	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;	process_command(&cmdtab, margc, margv);    }    fclose(rcfile);}#if defined(IP_OPTIONS) && defined(HAS_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. *	 */static unsigned long sourceroute(char *arg, char **cpp, int *lenp) {	static char lsr[44];	char *cp, *cp2, *lsrp, *lsrep;	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((unsigned long)-1);	if (*cpp != NULL && *lenp < 7)		return((unsigned long)-1);	/*	 * 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((unsigned long)-1);	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) != 0; 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 (inet_aton(cp, &sin_addr)) ;  /* nothing */		else if ((host = gethostbyname(cp))!=NULL) {		    if (host->h_length > (int)sizeof(sin_addr)) {			host->h_length = sizeof(sin_addr);		    }#if defined(h_addr)		    memcpy(&sin_addr, host->h_addr_list[0], host->h_length);#else		    memcpy(&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((unsigned long)-1);	}	if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {		*cpp = 0;		*lenp = 0;		return((unsigned long)-1);	}	*lsrp++ = IPOPT_NOP; /* 32 bit word align it */	*lenp = lsrp - *cpp;	return(sin_addr.s_addr);}#endif

⌨️ 快捷键说明

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