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

📄 utility.c

📁 linux下telnet服务端的源码实现
💻 C
📖 第 1 页 / 共 3 页
字号:
		    debug_output_data (" %s %d\r\n", cp, pointer[i]);		  break;		case SB:		  debug_output_data (" SB ");		  i++;		  j = k = i;		  while (j < length)		    {		      if (pointer[j] == SE)			{			  if (j+1 == length)			    break;			  if (pointer[j+1] == SE)			    j++;			  else			    break;			}		      pointer[k++] = pointer[j++];		    }		  printsub(0, &pointer[i], k - i);		  if (i < length)		    {		      debug_output_data (" SE");		      i = j;		    } else		      i = j - 1;		  debug_output_data ("\r\n");		  break;		default:		  debug_output_data (" %d", pointer[i]);		  break;		}	    }	  break;	}	break;      }    case TELOPT_XDISPLOC:      debug_output_data ("X-DISPLAY-LOCATION ");      switch (pointer[1])	{	case TELQUAL_IS:	  debug_output_data ("IS \"%.*s\"", length-2, (char *)pointer+2);	  break;	case TELQUAL_SEND:	  debug_output_data ("SEND");	  break;	default:	  debug_output_data ("- unknown qualifier %d (0x%x).",		       pointer[1], pointer[1]);	}      break;    case TELOPT_NEW_ENVIRON:      debug_output_data ("NEW-ENVIRON ");      goto env_common1;          case TELOPT_OLD_ENVIRON:      debug_output_data ("OLD-ENVIRON");    env_common1:      switch (pointer[1])	{	case TELQUAL_IS:	  debug_output_data ("IS ");	  goto env_common;	case TELQUAL_SEND:	  debug_output_data ("SEND ");	  goto env_common;	case TELQUAL_INFO:	  debug_output_data ("INFO ");	env_common:	  {	    register int noquote = 2;	    for (i = 2; i < length; i++ )	      {		switch (pointer[i])		  {		  case NEW_ENV_VAR:		    debug_output_data ("\" VAR " + noquote);		    noquote = 2;		    break;		    		  case NEW_ENV_VALUE:		    debug_output_data ("\" VALUE " + noquote);		    noquote = 2;		    break;		  case ENV_ESC:		    debug_output_data ("\" ESC " + noquote);		    noquote = 2;		    break;		    		  case ENV_USERVAR:		    debug_output_data ("\" USERVAR " + noquote);		    noquote = 2;		    break;		    		  default:		  def_case:		    if (isprint(pointer[i]) && pointer[i] != '"')		      {			if (noquote)			  {			    debug_output_data ("\"");			    noquote = 0;			  }			debug_output_datalen (&pointer[i], 1);		      }		    else		      {			debug_output_data ("\" %03o " + noquote,				     pointer[i]);			noquote = 2;		      }		    break;		  }	      }	    if (!noquote)	      debug_output_data ("\"");	    break;	  }	}      break;#if	defined(AUTHENTICATION)    case TELOPT_AUTHENTICATION:      debug_output_data ("AUTHENTICATION");      if (length < 2)	{	  debug_output_data (" (empty suboption??\?)");	  break;	}      switch (pointer[1])	{	case TELQUAL_REPLY:	case TELQUAL_IS:	  debug_output_data (" %s ", (pointer[1] == TELQUAL_IS) ?		       "IS" : "REPLY");	  if (AUTHTYPE_NAME_OK(pointer[2]))	    debug_output_data ("%s ", AUTHTYPE_NAME(pointer[2]));	  else	    debug_output_data ("%d ", pointer[2]);	  if (length < 3)	    {	      debug_output_data ("(partial suboption??\?)");	      break;	    }	  debug_output_data ("%s|%s",		       ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?		       "CLIENT" : "SERVER",		       ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?		       "MUTUAL" : "ONE-WAY");	  auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));	  debug_output_data ("%s", buf);	  break;	case TELQUAL_SEND:	  i = 2;	  debug_output_data (" SEND ");	  while (i < length)	    {	      if (AUTHTYPE_NAME_OK(pointer[i]))		debug_output_data ("%s ", AUTHTYPE_NAME(pointer[i]));	      else		debug_output_data ("%d ", pointer[i]);	      if (++i >= length)		{		  debug_output_data ("(partial suboption??\?)");		  break;		}	      debug_output_data ("%s|%s ",			   ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?			   "CLIENT" : "SERVER",			   ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?			   "MUTUAL" : "ONE-WAY");	      ++i;	    }	  break;	  	case TELQUAL_NAME:	  i = 2;	  debug_output_data (" NAME \"");	  debug_output_datalen (&pointer[i], length);	  i += length;	  debug_output_data ("\"");	  break;	default:	  for (i = 2; i < length; i++)	    debug_output_data (" ?%d?", pointer[i]);	  break;	}      break;#endif#ifdef	ENCRYPTION    case TELOPT_ENCRYPT:      debug_output_data ("ENCRYPT");      if (length < 2)	{	  debug_output_data (" (empty suboption??\?)");	  break;	}      switch (pointer[1])	{	case ENCRYPT_START:	  debug_output_data (" START");	  break;	case ENCRYPT_END:	  debug_output_data (" END");	  break;	  	case ENCRYPT_REQSTART:	  debug_output_data (" REQUEST-START");	  break;	  	case ENCRYPT_REQEND:	  debug_output_data (" REQUEST-END");	  break;	case ENCRYPT_IS:	case ENCRYPT_REPLY:	  debug_output_data (" %s ", (pointer[1] == ENCRYPT_IS) ?		       "IS" : "REPLY");	  if (length < 3)	    {	      debug_output_data (" (partial suboption??\?)");	      break;	    }	  if (ENCTYPE_NAME_OK(pointer[2]))	    debug_output_data ("%s ", ENCTYPE_NAME(pointer[2]));	  else	    debug_output_data (" %d (unknown)", pointer[2]);	  	  encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));	  debug_output_data ("%s", buf);	  break;	case ENCRYPT_SUPPORT:	  i = 2;	  debug_output_data (" SUPPORT ");	  while (i < length)	    {	      if (ENCTYPE_NAME_OK(pointer[i]))		debug_output_data ("%s ", ENCTYPE_NAME(pointer[i]));	      else		debug_output_data ("%d ", pointer[i]);	      i++;	    }	  break;	  	case ENCRYPT_ENC_KEYID:	  debug_output_data (" ENC_KEYID", pointer[1]);	  goto encommon;	  	case ENCRYPT_DEC_KEYID:	  debug_output_data (" DEC_KEYID", pointer[1]);	  goto encommon;	default:	  debug_output_data (" %d (unknown)", pointer[1]);	encommon:	  for (i = 2; i < length; i++) 	    debug_output_data (" %d", pointer[i]);	  break;	}      break;#endif	/* ENCRYPTION */    default:      if (TELOPT_OK(pointer[0]))	debug_output_data ("%s (unknown)", TELOPT(pointer[0]));      else	debug_output_data ("%d (unknown)", pointer[i]);      for (i = 1; i < length; i++) 	debug_output_data (" %d", pointer[i]);      break;    }  debug_output_data ("\r\n");}/* * Dump a data buffer in hex and ascii to the output data stream. */voidprintdata(register char *tag, register char *ptr, register int cnt){  register int i;  char xbuf[30];    while (cnt)    {      /* add a line of output */      debug_output_data ("%s: ", tag);      for (i = 0; i < 20 && cnt; i++)	{	  debug_output_data ("%02x", *ptr);	  xbuf[i] = isprint(*ptr) ? *ptr : '.';	  if (i % 2) 	    debug_output_data (" ");	  cnt--;	  ptr++;	}      xbuf[i] = '\0';      debug_output_data (" %s\r\n", xbuf );    }}#if defined(AUTHENTICATION) || defined(ENCRYPTION)intnet_write (unsigned char *str, int len){  return net_output_datalen (str, len);}voidnet_encrypt (){#ifdef	ENCRYPTION  char *s = (nclearto > nbackp) ? nclearto : nbackp;  if (s < nfrontp && encrypt_output)     (*encrypt_output)((unsigned char *)s, nfrontp - s);  nclearto = nfrontp;#endif /* ENCRYPTION */}inttelnet_spin(){  io_drain ();  return 0;}#endif/* ************************************************************************* *//* String expansion functions */#define EXP_STATE_CONTINUE 0#define EXP_STATE_SUCCESS  1#define EXP_STATE_ERROR    2struct line_expander{  int state;           /* Current state */  int level;           /* The nesting level */  char *source;        /* The source string */  char *cp;            /* Current position in the source */  struct obstack stk;  /* Obstack for expanded version */};static char *_var_short_name P((struct line_expander *exp));static char *_var_long_name P((struct line_expander *exp,			       char *start, int length));static char *_expand_var P((struct line_expander *exp));static void _expand_cond P((struct line_expander *exp));static void _skip_block P((struct line_expander *exp));static void _expand_block P((struct line_expander *exp));     /* Expand a variable referenced by its short one-symbol name.   Input: exp->cp points to the variable name.   FIXME: not implemented */char *_var_short_name (struct line_expander *exp){  char *q;  char timebuf[64];  time_t t;    switch (*exp->cp++)    {    case 'a':#ifdef AUTHENTICATION      if (auth_level >= 0 && autologin == AUTH_VALID)	return xstrdup ("ok");#endif      return NULL;          case 'd':      time (&t);      strftime(timebuf, sizeof (timebuf),	       "%l:%M%P on %A, %d %B %Y", localtime(&t));      return xstrdup (timebuf);    case 'h':      return xstrdup (remote_hostname);          case 'l':      return xstrdup (local_hostname);	    case 't':      q = strchr (line + 1, '/');      if (q)	q++;      else	q = line;      return xstrdup (q);          case 'T':      return terminaltype ? xstrdup (terminaltype) : NULL;	    case 'u':      return user_name ? xstrdup (user_name) : NULL;    default:      exp->state = EXP_STATE_ERROR;      return NULL;    }}/* Expand a variable referenced by its long name.   Input: exp->cp points to initial '('   FIXME: not implemented */char *_var_long_name (struct line_expander *exp, char *start, int length){  exp->state = EXP_STATE_ERROR;  return NULL;}/* Expand a variable to its value.   Input: exp->cp points one character _past_ % (or ?) */char *_expand_var (struct line_expander *exp){  char *p;  switch (*exp->cp)    {    case '{':      /* Collect variable name */      for (p = ++exp->cp; *exp->cp && *exp->cp != '}'; exp->cp++)	;      if (*exp->cp == 0)	{	  exp->cp = p;	  exp->state = EXP_STATE_ERROR;	  break;	}      p = _var_long_name (exp, p, exp->cp - p);      exp->cp++;      break;    default:      p = _var_short_name (exp);      break;    }  return p;}/* Expand a conditional block. A conditional block is:       %?<var>{true-stmt}[{false-stmt}]   <var> may be either a one-symbol variable name or (string). The latter   is not handled yet.   On input exp->cp points to % character */void_expand_cond (struct line_expander *exp){  char *p;  if (*++exp->cp == '?')    {      /* condition */      exp->cp++;      p = _expand_var (exp);      if (p)	{	  _expand_block (exp);	  _skip_block (exp);	}      else	{	  _skip_block (exp);	  _expand_block (exp);	}      free (p);    }  else    {      p = _expand_var (exp);      if (p)	obstack_grow (&exp->stk, p, strlen (p));      free (p);    }}/* Skip the block. If the exp->cp does not point to the beginning of a   block ({ character), the function does nothing */void_skip_block (struct line_expander *exp){  int level = exp->level;  if (*exp->cp != '{')    return;  for (; *exp->cp; exp->cp++)    {      switch (*exp->cp)	{	case '{':	  exp->level++;	  break;	  	case '}':	  exp->level--;	  if (exp->level == level)	    {	      exp->cp++;	      return;	    }	}    }}/* Expand a block within the formatted line. Stops either when end of source   line was reached or the nesting reaches the initial value */void_expand_block (struct line_expander *exp){  int level = exp->level;  if (*exp->cp == '{')    {      exp->level++;      exp->cp++;/*FIXME?*/    }  while (exp->state == EXP_STATE_CONTINUE)    {      for (; *exp->cp && *exp->cp != '%'; exp->cp++)	{	  switch (*exp->cp)	    {	    case '{':	      exp->level++;	      break;	      	    case '}':	      exp->level--;	      if (exp->level == level)		{		  exp->cp++;		  return;		}	      break;	      	    case '\\':	      exp->cp++;	      break;	    }	  obstack_1grow (&exp->stk, *exp->cp);	}            if (*exp->cp == 0)	{	  obstack_1grow (&exp->stk, 0);	  exp->state = EXP_STATE_SUCCESS;	  break;	}      else if (*exp->cp == '%' && exp->cp[1] == '%')	{	  obstack_1grow (&exp->stk, *exp->cp);	  exp->cp += 2;	  continue;	}      _expand_cond (exp);    }}/* Expand a format line */char *expand_line (const char *line){  char *p = NULL;  struct line_expander exp;  exp.state = EXP_STATE_CONTINUE;  exp.level = 0;  exp.source = line;  exp.cp = line;  obstack_init (&exp.stk);  _expand_block (&exp);  if (exp.state == EXP_STATE_SUCCESS)    p = xstrdup (obstack_finish (&exp.stk));  else    {      syslog (LOG_ERR, "can't expand line: %s", line);      syslog (LOG_ERR, "stopped near %s", exp.cp ? exp.cp : "(END)");    }  obstack_free (&exp.stk, NULL);  return p;}      

⌨️ 快捷键说明

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