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

📄 ckcnet.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 4 页
字号:
	debug(F101,"netflui 2","",n);	/* ... */	n = socket_read(ttyfd,ttibuf,n) ; /* into our buffer */	if (n >= 0) ttibuf[n] = '\0';	debug(F111,"netflui 3",ttibuf,n);	ttibuf[0] = '\0';    }#else/*  It seems the UNIX ioctl()s don't do the trick, so we have to read the  stuff ourselves.  This should be pretty much portable, if not elegant.*/    if ((n = ttchk()) > 0) {	debug(F101,"netflui","",n);	while ((n--) && ttinc(0) > -1) ; /* Don't worry, it's buffered. */    }#endif /* VMSTCPIP */    return(0);}#ifdef TNCODE				/* Compile in telnet support code *//* TCP/IP Telnet negotiation support code */static int sgaflg = 0;			/* telnet SGA flag */static int wttflg = 0;			/* telnet terminal type flag */#ifndef TELCMDSchar *telcmds[] = {    "SE", "NOP", "DMARK", "BRK",  "IP",   "AO", "AYT",  "EC",    "EL", "GA",  "SB",    "WILL", "WONT", "DO", "DONT", "IAC",};int ntelcmds = sizeof(telcmds) / sizeof(char *);#endif /* TELCMDS */#ifndef TELOPTSchar *telopts[] = {	"BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME",	"STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",	"NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS",	"NAOVTD", "NAOLFD", "EXTEND ASCII", "LOGOUT", "BYTE MACRO",	"DATA ENTRY TERMINAL", "SUPDUP", "SUPDUP OUTPUT",	"SEND LOCATION", "TERMINAL TYPE", "END OF RECORD"#ifdef TELOPT_TUID	,"TACACS UID"#ifdef TELOPT_OUTMRK	,"OUTPUT MARKING"#ifdef TELOPT_TTYLOC	,"TTYLOC"#ifdef TELOPT_3270REGIME	,"3270 REGIME"#ifdef TELOPT_X3PAD	,"X.3 PAD"#ifdef TELOPT_NAWS	,"NAWS"#ifdef TELOPT_TSPEED	,"TSPEED"#ifdef TELOPT_LFLOW	,"LFLOW"#ifdef TELOPT_LINEMODE	,"LINEMODE"#endif#endif#endif#endif#endif#endif#endif#endif#endif};#endif /* TELOPTS */int ntelopts = sizeof(telopts) / sizeof(char *);#endif /* TNCODE *//* Send network BREAK *//*  Returns -1 on error, 0 if nothing happens, 1 if BREAK sent successfully.*/intnetbreak() {    CHAR buf[3];    if (ttnet == NET_TCPB) {	if (ttnproto == NP_TELNET) {#ifdef TNCODE	    buf[0] = (CHAR) IAC; buf[1] = (CHAR) BREAK;	    if (ttol(buf,2) < 2) return(-1);	    debug(F101,"telnet BREAK ok","",BREAK);	    return(1);#else	    debug(F100,"netbreak no TNCODE","",0);	    return(0);#endif /* TNCODE */	}	/* Insert other TCP/IP protocols here */    }    /* Insert other networks here */    return(0);}/* Send a telnet option, avoid loops. *//* Returns 1 if command was sent, 0 if not, -1 on error */inttn_sopt(cmd,opt) int cmd, opt; {	/* TELNET SEND OPTION */    CHAR buf[4];    int n;    if (ttnet != NET_TCPB) return(0);	/* Must be TCP/IP */    if (ttnproto != NP_TELNET) return(0); /* Must be telnet protocol */    n = cmd - SE;    if (n < 0 || n > ntelcmds) return(0);#ifdef TNCODE    buf[0] = (CHAR) IAC;    buf[1] = (CHAR) cmd & 0xff;    buf[2] = (CHAR) opt & 0xff;    if (ttol(buf,3) < 3)      return(-1);    debug(F111,"telnet cmd >",telcmds[n],cmd);    debug(F111,"telnet opt >",	  (opt < ntelopts) ? telopts[opt] : "UNKNOWN", opt );    if (debses && cmd != SB)      printf("[%s %s]",telcmds[n],	     (opt < ntelopts) ? telopts[opt] : "UNKNOWN");    return(1);#else    debug(F100,"tn_sopt no TNCODE","",0);    return(0);#endif /* TNCODE */}/* Initialize a telnet connection. *//* Returns -1 on error, 0 if nothing happens, 1 if init msgs sent ok */inttn_ini() {#ifndef TNCODE    debug(F100,"tn_ini no TNCODE","",0);    return(0);#else /* TELNET protocol support */    debug(F101,"tn_ini ttnproto","",ttnproto);    debug(F101,"tn_ini tn_init","",tn_init);    if (ttnet != NET_TCPB)		/* Make sure connection is TCP/IP. */      return(0);    if (tn_init)			/* Have we done this already? */      return(0);			/* Don't do it again. */    debug(F101,"tn_ini tn_duplex","",tn_duplex);    duplex = tn_duplex;			/* Assume local echo. */    sgaflg = 0;				/* Assume Go-Ahead suppressed. */    wttflg = 0;				/* Did not send WILL TERM TYPE yet. */    if (ttnproto == NP_NONE) {		/* If not talking to a telnet port, */	ttnproto = NP_TELNET;		/* pretend it's telnet anyway, */	tn_init = 1;			/* but don't send initial options. */	debug(F100,"tn_ini skipping telnet negotiations","",0);	return(0);    }    /* Talking to telnet port, so send WILL TERMINAL TYPE and DO SGA */    if (tn_sopt(WILL,TELOPT_TTYPE) < 0)	/* Will send terminal type. */      return(-1);    wttflg = 1;				/* Remember I said I would. */    if (tn_sopt(DO,TELOPT_SGA) < 0)	/* Please suppress go-ahead. */      return(-1);#ifdef COMMENT    if (tn_sopt(DO,TELOPT_ECHO) < 0)	/* Ask the server to echo, since */      return(-1);			/* I'm assuming it will. */#endif /* COMMENT */    tn_init = 1;			/* Set telnet-initialized flag. */    /* Don't send anthing else! */    debug(F101,"tn_ini duplex","",duplex);    return(1);#endif /* TNCODE */}/*  Process in-band Telnet negotiation characters from the remote host.  Call with the telnet IAC character and the current duplex setting  (0 = remote echo, 1 = local echo).  Returns:    3 if server has sent us a quoted IAC    2 if local echo must be changed to remote    1 if remote echo must be changed to local    0 if nothing happens or no action necessary   -1 on failure (= internal or i/o error)*/#define TSBUFSIZ 41char sb[TSBUFSIZ];			/* Buffer for subnegotiations */int#ifdef CK_ANSIC				/* TELNET DO OPTION */tn_doop( CHAR z, int echo, int (*fn)(int) )#elsetn_doop(z, echo, fn) CHAR z; int echo; int (*fn)();#endif /* CK_ANSIC *//* tn_doop */ {    int c, x, y, n, m, flag;#ifndef TNCODE    debug(F100,"tn_doop no TNCODE","",0);    return(0);#else    if (z != (CHAR) IAC) {	debug(F101,"tn_doop bad call","",z);	return(-1);    }    if (ttnet != NET_TCPB) return(0);    if (ttnproto != NP_TELNET) return(0); 	/* Check protocol *//* Have IAC, read command character. */    c = (*fn)(0) & 0xff;		/* Read command character */    m = c - SE;				/* Check validity */    if (m < 0 || m > ntelcmds) {	debug(F101,"tn_doop bad cmd","",c);	return(0);    }    if (seslog) {			/* Copy to session log, if any. */	if (zchout(ZSFILE, (char) z) < 0) seslog = 0; /* Log IAC. */	else if (zchout(ZSFILE, (char) c) < 0) seslog = 0; /* Log command */    }    debug(F111,"telnet cmd <",telcmds[m],c); /* Debug log. */    if (c == (CHAR) IAC) return(3);	/* Quoted IAC */    if (c < SB) return(0);		/* Other command with no arguments. *//* SB, WILL, WONT, DO, or DONT need more bytes... */    if ((x = (*fn)(0)) < 0) return(-1);	/* Get the option. */    x &= 0xff;				/* Trim to 8 bits. */    debug(F111,"telnet opt <",	  (x < ntelopts) ? telopts[x] : "UNKNOWN", x );    if (seslog)				/* Session log */      if (zchout(ZSFILE, (char) x) < 0) seslog = 0;    /* Now handle the command */    if (debses && c != SB) 		/* Debug to screen. */      printf("<%s %s>",telcmds[m],	     (x < ntelopts) ? telopts[x] : "UNKNOWN" );    switch (x) {      case TELOPT_ECHO:			/* ECHO negotiation. */	switch (c) {			/* Command */	  case WILL:			/* Host says it will echo. */	    if (echo)			/* Only reply if change required. */	      return((tn_sopt(DO,x) < 0) ? -1 : 2); /* Please do. */	    else return(0);		/* Otherwise no change. */	  case WONT:			/* Host says it won't echo. */	    if (!echo)			/* If I'm full duplex */	      return ((tn_sopt(DONT,x) < 0) ? -1 : 1); /* Switch to half */	    else return(0);		/* Otherwise, no change.  */	  case DO:			/* Host wants me to echo */	    if (tn_sopt(WONT,x) < 0)	/* but the client never echoes */	      return(-1);		/* back to the server. */	  default:			/* Don't reply to anything else */	    return(0);	}      case TELOPT_SGA:			/* Suppress Go-Ahead */	switch (c) {			/* Command... */	  case WONT:			/* Host says it won't. */	    if (!sgaflg) {		sgaflg = 1;		/* Remember. */		if (tn_sopt(DONT,x) < 0) /* acknowledge, */		  return(-1);	    }	    return(echo ? 0 : 1);	/* Switch to half duplex */	  case WILL:			/* Server says it will SGA */	    if (sgaflg) {		/* ACK only if necessary */		if (tn_sopt(DO,x) < 0)		  return(-1);		sgaflg = 0;		/* Remember new SGA state. */	    }	    return(0);			/* But don't change echo state. */	}#ifdef TELOPT_TTYPE      case TELOPT_TTYPE:		/* Terminal Type */	switch (c) {	  case DO:			/* DO terminal type. */	    if (wttflg == 0) {		/* If I haven't said so before, */		if (tn_sopt((CHAR)WILL,x) < 0) /* say I'll send it if asked. */		  return(-1);		wttflg++;	    }	    return(0);	  case SB:	    debug(F100,"TELNET subnegotiation:","",0);	    n = flag = 0;		/* Flag for when done reading SB */	    while (n < TSBUFSIZ) {	/* Loop looking for IAC SE */		if ((y = (*fn)(0)) < 0)	/* Read a byte */		  return(-1);		y &= 0xff;		/* Make sure it's just 8 bits. */		sb[n++] = y;		/* Deposit in buffer. */		if (seslog)		/* Take care of session log */		  if (zchout(ZSFILE, (char) y) < 0)		    seslog = 0;		if (y == IAC) {		/* If this is an IAC */		    if (flag) {		/* If previous char was IAC */			n--;		/* it's quoted, keep one IAC */			flag = 0;	/* and turn off the flag. */		    } else flag = 1;	/* Otherwise set the flag. */		} else if (flag) { 	/* Something else following IAC */		    if (y != SE)	/* If not SE, it's a protocol error */		      flag = 0;		    break;		}	    }	    if (!flag) {		/* Make sure we got a valid SB */		debug(F100, "TELNET Subnegotian prematurely broken", "",0);		return(-1);	    }	    if (debses) {		/* Debug to screen. */		int i;		printf("<SB %s ",telopts[TELOPT_TTYPE]);		for (i = 0; i < n-2; i++) printf("%02x",sb[i]);		printf(" IAC SE>");	    }	    debug(F101,"TELNET suboption<","",sb[0]);	    if (sb[0] == 1) {		/* SEND terminal type? */		if (tn_sttyp() < 0)	/* Yes, so send it. */		  return(-1);	    }	  default:			/* Others, ignore */	    return(0);	}#endif /* TELOPT_TTYPE */      default:				/* All others: refuse */	switch(c) {	  case WILL:			/* You will? */	    if (tn_sopt(DONT,x) < 0)	/* Please don't. */	      return(-1);		/* (Could this cause a loop?) */	    break;	  case DO:			/* You want me to? */	    if (tn_sopt(WONT,x) < 0)	/* I won't. */	      return(-1);	    break;	  case DONT:			/* You don't want me to? */	    if (tn_sopt(WONT,x) < 0)	/* I won't. */	      return(-1);		/* (Could this cause a loop?) */	  case WONT:			/* You won't? */	    break;			/* I didn't want you to. */	}				/* Anything else, treat as user data */	return(0);    }#endif /* TNCODE */}/* Telnet send terminal type *//* Returns -1 on error, 0 if nothing happens, 1 if type sent successfully */inttn_sttyp() {				/* Send telnet terminal type. */#ifndef TNCODE    debug(F100,"tn_sttyp no TNCODE","",0);    return(0);#else    char *ttn; int ttl, i;		/* Name & length of terminal type. */    if (ttnet != NET_TCPB) return(0);    if (ttnproto != NP_TELNET) return(0);    ttn = NULL;    if (tn_term) {			/* Terminal type override? */	debug(F110,"tn_sttyp",tn_term,0);	if (*tn_term) ttn = tn_term;    } else debug(F100,"tn_sttyp no term override","",0);#ifndef datageneral    if (!ttn)				/* If no override, */      ttn = getenv("TERM");		/* get it from the environment. */#endif /* datageneral */    if ((ttn == ((char *)0)) || ((ttl = (int)strlen(ttn)) >= TSBUFSIZ)) {	ttn = "UNKNOWN";	ttl = 7;    }    sb[0] = IAC;			/* I Am a Command */    sb[1] = SB;				/* Subnegotiation */    sb[2] = TELOPT_TTYPE;		/* Terminal Type */    sb[3] = (CHAR) 0;			/* Is... */    for (i = 4; *ttn; ttn++,i++)	/* Copy and uppercase it */      sb[i] = (islower(*ttn)) ? toupper(*ttn) : *ttn;    ttn = sb;				/* Point back to beginning */    sb[i++] = IAC;			/* End of Subnegotiation */    sb[i++] = SE;			/* marked by IAC SE */    if (ttol((CHAR *)sb,i) < 0)		/* Send it. */      return(-1);#ifdef DEBUG    sb[i-2] = '\0';			/* For debugging */    debug(F111,"telnet SB sent ttype",sb+4,ttl);#endif /* DEBUG */    if (debses)				/* Debug to screen. */      printf("[SB TERMINAL TYPE 00 %s IAC SE]",sb+4);    return(1);#endif /* TNCODE */}#ifdef SUNX25/*  SunLink X.25 support by Marcello Frutig, Catholic University,  Rio de Janeiro, Brazil, 1990.*//* PAD X.3, X.28 and X.29 support */static CHAR x29err [MAXPADPARMS+3] = { X29_ERROR, INVALID_PAD_PARM, '\0' };/* Initialize PAD */extern CHAR padparms[MAXPADPARMS+1];VOIDinitpad() {  padparms[PAD_BREAK_CHARACTER]        = 0;  /* Break character */  padparms[PAD_ESCAPE]                 = 1;  /* Escape permitted */  padparms[PAD_ECHO]                   = 1;  /* Kermit PAD does echo */  padparms[PAD_DATA_FORWARD_CHAR]      = 2;  /* forward character CR */  padparms[PAD_DATA_FORWARD_TIMEOUT]   = 0;  /* no timeout forward condition */  padparms[PAD_FLOW_CONTROL_BY_PAD]    = 0;  /* not used */  padparms[PAD_SUPPRESSION_OF_SIGNALS] = 1;  /* allow PAD service signals */  padparms[PAD_BREAK_ACTION]           = 21; /* brk action: INT pk + brk ind*/

⌨️ 快捷键说明

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