commands.c

来自「linux下常用的网络工具的代码」· C语言 代码 · 共 2,831 行 · 第 1/5 页

C
2,831
字号
#ifdef IPV6	  hints.ai_family = AF_INET;#endif	  continue;	}      if (hostp == 0)	{	  hostp = *argv++;	  --argc;	  continue;	}      if (portp == 0)	{	  portp = *argv++;	  --argc;	  continue;	}    usage:      printf("usage: %s [-4] [-6] [-l user] [-a] host-name [port]\n", cmd);      return 0;    }  if (hostp == 0)    goto usage;  if (!portp)    {      portp = "telnet";      telnetport = 1;    }  else    {      if (*portp == '-')	{	  portp++;	  telnetport = 1;	}      else	telnetport = 0;    }  if (hostname)    free (hostname);  hostname = malloc (strlen (hostp) + 1);  if (hostname)    strcpy (hostname, hostp);  else    {      printf ("Can't allocate memory to copy hostname\n");      return 0;    } #ifdef IPV6#ifdef AI_ADDRCONFIG  hints.ai_flags = AI_ADDRCONFIG;#endif  hints.ai_socktype = SOCK_STREAM;  err = getaddrinfo (hostp, portp, &hints, &result);  if (err)    {      const char *errmsg;      if (err == EAI_SYSTEM)	errmsg = strerror (errno);      else	errmsg = gai_strerror (err);            printf ("%s/%s: lookup failure: %s\n", hostp, portp, errmsg);      return 0;    }  aip = result;  do    {      char buf[256];      err = getnameinfo (aip->ai_addr, aip->ai_addrlen, buf, sizeof (buf),			 NULL, 0, NI_NUMERICHOST);      if (err)	{	  /* I don't know how thing can happen, but we just handle it.  */	  const char *errmsg;	  	  if (err == EAI_SYSTEM)	    errmsg = strerror (errno);	  else	    errmsg = gai_strerror (err);	  	  printf ("getnameinfo error: %s\n", errmsg);	  return 0;	}            printf ("Trying %s...\n", buf);      net = socket (aip->ai_family, SOCK_STREAM, 0);      if (net < 0)	{	  perror("telnet: socket");	  return 0;	}      if (debug)	{	  err = setsockopt (net, SOL_SOCKET, SO_DEBUG, &on, sizeof(on));	  if (err < 0)	    perror("setsockopt (SO_DEBUG)");	}      err = connect (net, (struct sockaddr *)aip->ai_addr, aip->ai_addrlen);      if (err < 0)	{	  if (aip->ai_next)	    {	      perror ("Connection failed");	      aip = aip->ai_next;	      close (net);	      continue;	    }	    	  perror("telnet: Unable to connect to remote host");	  return 0;	}            connected++;#if	defined(AUTHENTICATION) || defined(ENCRYPTION)      auth_encrypt_connect(connected);#endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */    } while (!connected);  freeaddrinfo (result);#else /* !IPV6 */  temp = inet_addr (hostp);  if (temp != (in_addr_t) -1)    {      sin.sin_addr.s_addr = temp;      sin.sin_family = AF_INET;    }  else    {      host = gethostbyname (hostp);      if (host)	{	  sin.sin_family = host->h_addrtype;	  memmove (&sin.sin_addr, host->h_addr_list[0], host->h_length);	}      else	{	  printf ("Can't lookup hostname %s\n", hostp);	  return 0;	}    }    sin.sin_port = atoi (portp);  if (sin.sin_port == 0)    {      sp = getservbyname (portp, "tcp");      if (sp == 0)	{	  printf ("tcp/%s: unknown service\n", portp);	  return 0;	}      sin.sin_port = sp->s_port;    }  else    sin.sin_port = htons (sin.sin_port);    printf("Trying %s...\n", inet_ntoa(sin.sin_addr));  do    {      net = socket(AF_INET, SOCK_STREAM, 0);      if (net < 0)	{	  perror("telnet: socket");	  return 0;	}#if defined(IPPROTO_IP) && defined(IP_TOS)      {#ifdef IPTOS_LOWDELAY	const int tos = IPTOS_LOWDELAY;#else	const int tos = 0x10;#endif	err = setsockopt (net, IPPROTO_IP, IP_TOS,			  (char *)&tos, sizeof(tos));	if (err < 0 && errno != ENOPROTOOPT)	  perror("telnet: setsockopt (IP_TOS) (ignored)");      }#endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */      if (debug && setsockopt(net, SOL_SOCKET, SO_DEBUG, &on, sizeof(on)) < 0)	perror("setsockopt (SO_DEBUG)");      if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0)	{	  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++;	      memmove((caddr_t)&sin.sin_addr,		      host->h_addr_list[0], host->h_length);	      close (net);	      continue;	    }	  perror("telnet: Unable to connect to remote host");	  return 0;	}      connected++;#if	defined(AUTHENTICATION) || defined(ENCRYPTION)      auth_encrypt_connect(connected);#endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */    } while (connected == 0);#endif /* !IPV6 */  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");    }  call(status, "status", "notmuch", 0);  if (setjmp(peerdied) == 0)    telnet(user);  close (net);  ExitString("Connection closed by foreign host.\n",1);  /*NOTREACHED*/  return 0;}#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 characters ('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(AUTHENTICATION)	authhelp[] =	"turn on (off) authentication ('auth ?' for more)",#endif#ifdef	ENCRYPTION	encrypthelp[] =	"turn on (off) encryption ('encrypt ?' for more)",#endif	/* ENCRYPTION */#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)";static 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(AUTHENTICATION)	{ "auth",	authhelp,	auth_cmd,	0 },#endif#ifdef	ENCRYPTION	{ "encrypt",	encrypthelp,	encrypt_cmd,	0 },#endif	/* ENCRYPTION */#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*/    static#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__call(intrtn_t routine, ...)#elsecall(va_alist)    va_dcl#endif{    va_list ap;#if !(defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__)    intrtn_t routine;#endif    char *args[100];    int argno = 0;#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__    va_start(ap, routine);#else    va_start(ap);    routine = (va_arg(ap, intrtn_t));#endif    while ((args[argno++] = va_arg(ap, char *)) != 0) {	;    }    va_end(ap);    return (*routine)(argno-1, args);}static Command *getcmd(char *name){    Command *cm;    if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command)))	return cm;    return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));}voidcommand(int top, char *tbuf, int cnt){    register Command *c;    setcommandmode();    if (!top) {	putchar('\n');#if	defined(unix)    } else {	signal(SIGINT, SIG_DFL);	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)) {		    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. */static inthelp(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];intcmdrc(char *m1, char *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);    }    

⌨️ 快捷键说明

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