📄 ckctel.c
字号:
) { if ( notquiet ) printf("?Telnet waiting for DO %s subnegotiation\r\n", TELOPT(TELOPT_KERMIT)); debug(F111,"tn_outst","U kermit in progress",TELOPT_KERMIT); outstanding = 1; } }#endif /* IKS_OPTION */ return(outstanding);}/* tn_wait() -- Wait for response to Telnet negotiation. *//* Wait for up to <timeout> seconds for the response to arrive. Place all non-telnet data into Telnet Wait Buffer. If response does arrive return 1, else return 0.*/#ifndef TN_WAIT_BUF_SZ#define TN_WAIT_BUF_SZ 4096#endif /* TN_WAIT_BUF_SZ */static char tn_wait_buf[TN_WAIT_BUF_SZ];static int tn_wait_idx = 0;#ifndef TN_TIMEOUT#define TN_TIMEOUT 120#endif /* TN_TIMEOUT */static int tn_wait_tmo = TN_TIMEOUT;#ifdef CKSPINNERVOIDprtwait(state) int state; { switch (state % 4) { case 0: printf("/"); break; case 1: printf("-"); break; case 2: printf("\\"); break; case 3: printf("|"); break; }}#endif /* CKSPINNER */static int nflag = 0;int#ifdef CK_ANSICtn_wait(char * where)#elsetn_wait(where) char * where;#endif /* CK_ANSIC *//* tn_wait */ { extern int ckxech, local; int ch = 0, try = 0, count = 0; int outstanding; debug(F110,"tn_wait waiting for",where,0); tn_wait_tmo = TN_TIMEOUT; debug(F111,"tn_wait","timeout",tn_wait_tmo); outstanding = tn_outst(0); /* The following is meant to be !(||). We only want to return */ /* immediately if both the tn_wait_flg && tn_outst() are false */ if (!(outstanding || tn_wait_flg)) /* If no need to wait */ return(1); /* Don't. */ if (tn_deb || debses) tn_debug("<wait for outstanding negotiations>"); if (!sstelnet && !quiet) {#ifdef CKSPINNER prtwait(try);#endif /* CKSPINNER */ } /* Wait up to TN_TIMEOUT sec for responses to outstanding telnet negs */ while ((tn_wait_idx < TN_WAIT_BUF_SZ) && (count || ((outstanding || try == 0) && ttchk() >= 0)) ) {#ifdef NTSIG ck_ih();#endif /* NTSIG */ ch = ttinc(1); if (ch == -1) { /* Timed out */ try++; if (!sstelnet && !quiet) { /* Let user know... */#ifdef CKSPINNER printf("\b"); prtwait(try);#else if (nflag == 0) { printf(" Negotiations."); nflag++; } if (nflag > 0) { printf("."); nflag++; fflush(stdout); }#endif /* CKSPINNER */ } if ( try > tn_wait_tmo ) { if (!sstelnet) { printf( "\r\n?Telnet Protocol Timeout - connection closed\r\n"); if (tn_deb || debses) tn_debug( "<telnet protocol timeout - connection closed>"); tn_outst(1); } /* if we do not close the connection, then we will block */ /* the next time we hit a wait. and if we don't we will */ /* do the wrong thing if the host sends 0xFF and does */ /* not intend it to be an IAC. */ ttclos(0); whyclosed = WC_TELOPT; return(-1); } continue; } else if (ch < -1) { printf("\r\n?Connection closed by peer.\r\n"); if (tn_deb || debses) tn_debug("<connection closed by peer>"); return(-1); } switch (ch) { case IAC:#ifdef CKSPINNER if (!sstelnet && !quiet) printf("\b");#endif /* CKSPINNER */ ch = tn_doop((CHAR)(ch & 0xff),inserver?ckxech:duplex,ttinc);#ifdef CKSPINNER if (!sstelnet && !quiet) prtwait(try);#endif /* CKSPINNER */ debug(F101,"tn_wait tn_doop","",ch); switch (ch) { case 1: duplex = 1; /* Turn on echoing */ if (inserver) ckxech = 1; break; case 2: duplex = 0; /* Turn off echoing */ if (inserver) ckxech = 0; break; case 3: tn_wait_buf[tn_wait_idx++] = IAC; break; case 4: /* IKS event */ case 6: /* Logout */ break; case -1: printf("?Telnet Option negotiation error.\n"); if (tn_deb || debses) tn_debug("<Telnet Option negotiation error>"); return(-1); case -2: printf("?Connection closed by peer.\n"); if (tn_deb || debses) tn_debug("<Connection closed by peer>"); return(-2); default: if (ch < 0) { if (tn_deb || debses) tn_debug("<Unknown connection error>"); return(ch); } } /* switch */ break; default: tn_wait_buf[tn_wait_idx++] = (CHAR)(ch & 0xff); } /* switch */ outstanding = tn_outst(0); count = ttchk(); if (!try) try++; } /* while */ if (tn_wait_idx == TN_WAIT_BUF_SZ) { if (tn_deb || debses) tn_debug("<Telnet Wait Buffer filled>"); return(0); } if (!sstelnet && !quiet) {#ifdef CKSPINNER printf("\b \b");#else if (nflag > 0) { printf(" (OK)\n"); nflag = -1; }#endif /* CKSPINNER */ } if (tn_deb || debses) tn_debug("<no outstanding negotiations>"); return(0);}/* Push data from the Telnet Wait Buffer into the I/O Queue *//* Return 1 on success */inttn_push() {#ifdef NETLEBUF extern int tt_push_inited;#endif /* NETLEBUF */ if (tn_wait_idx) { hexdump((CHAR *)"tn_push",tn_wait_buf,tn_wait_idx);#ifdef NETLEBUF if (!tt_push_inited) /* Local handling */ le_init(); le_puts((CHAR *)tn_wait_buf,tn_wait_idx);#else /* External handling... */#ifdef OS2 /* K95 has its own way */ le_puts((CHAR *)tn_wait_buf,tn_wait_idx);#else#ifdef TTLEBUF /* UNIX, etc */ le_puts((CHAR *)tn_wait_buf,tn_wait_idx);#else/* If you see this message in AOS/VS, OS-9, VOS, etc, you need to copy the #ifdef TTLEBUF..#endif code from ckutio.c to the corresponding places in your ck?tio.c module.*/ printf("tn_push called but not implemented - data lost.\n");#endif /* NETLEBUF */#endif /* UNIX */#endif /* OS2 */ tn_wait_idx = 0; } tn_wait_tmo = TN_TIMEOUT; /* Reset wait timer stats */ return(1);}/* T N _ S O P T *//* Sends a telnet option, avoids 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[5]; if (ttnet != NET_TCPB) return(-1); /* Must be TCP/IP */ if (ttnproto != NP_TELNET) return(-1); /* Must be telnet protocol */ if (!TELCMD_OK(cmd)) return(-1); if (TELOPT_OK(opt)) { if (cmd == DO && TELOPT_UNANSWERED_DO(opt)) return(0); if (cmd == WILL && TELOPT_UNANSWERED_WILL(opt)) return(0); if (cmd == DONT && TELOPT_UNANSWERED_DONT(opt)) return(0); if (cmd == WONT && TELOPT_UNANSWERED_WONT(opt)) return(0); }#ifdef CK_SSL if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) { return(0); }#endif /* CK_SSL */ if (cmd == DO && opt == TELOPT_AUTHENTICATION) buf[0] = 0; if (tn_infinite && TELOPT_OK(opt)) { /* See comment above about */ int index = TELOPT_INDEX(opt); /* preventing infinite loops */ int m = cmd - WILL; if (tncnts[index][m] > MAXTNCNT) { if (tn_deb || debses || deblog) { sprintf(tn_msg,"TELNET negotiation loop %s %s", TELCMD(cmd), TELOPT(opt)); debug(F101,tn_msg,"",opt); if (tn_deb || debses) tn_debug(tn_msg); } return(0); } tncnts[index][m]++; tncnts[index][tnopps[m]] = 0; } buf[0] = (CHAR) IAC; buf[1] = (CHAR) (cmd & 0xff); buf[2] = (CHAR) (opt & 0xff); buf[3] = (CHAR) 0; if ((tn_deb || debses || deblog) && cmd != SB) { sprintf(tn_msg,"TELNET SENT %s %s",TELCMD(cmd), TELOPT(opt)); debug(F101,tn_msg,"",opt); } if (ttol(buf,3) < 3) { return(-1); } /* Only display the command if it was actually sent. */ if ((tn_deb || debses) && cmd != SB) tn_debug(tn_msg); if (TELOPT_OK(opt)) { if (cmd == DONT && TELOPT_UNANSWERED_DO(opt)) TELOPT_UNANSWERED_DO(opt) = 0; if (cmd == WONT && TELOPT_UNANSWERED_WILL(opt)) TELOPT_UNANSWERED_WILL(opt) = 0; if (cmd == DO && TELOPT_UNANSWERED_DONT(opt)) TELOPT_UNANSWERED_DONT(opt) = 0; if (cmd == WILL && TELOPT_UNANSWERED_WONT(opt)) TELOPT_UNANSWERED_WONT(opt) = 0; } return(1);}/* Send a telnet sub-option *//* Returns 1 if command was sent, 0 if not, -1 on error */inttn_ssbopt(opt,sub,data,len) int opt, sub; CHAR * data; int len; { CHAR buf[256]; int n,m; if (ttnet != NET_TCPB) return(0); /* Must be TCP/IP */ if (ttnproto != NP_TELNET) return(0); /* Must be telnet protocol */ if (!TELOPT_OK(opt)) return(-1); if (len < 0 || len > 250) { debug(F111,"Unable to Send TELNET SB - data too long","len",len); return(-1); /* Data too long */ }#ifdef CK_SSL if (TELOPT_SB(TELOPT_START_TLS).start_tls.me_follows) { if (ttchk() < 0) return(-1); else return(1); }#endif /* CK_SSL */ if (!data) len = 0; buf[0] = (CHAR) IAC; buf[1] = (CHAR) (SB & 0xff); buf[2] = (CHAR) (opt & 0xff); buf[3] = (CHAR) (sub & 0xff); if (data && len > 0) { memcpy(&buf[4],data,len); } buf[4+len] = (CHAR) IAC; buf[5+len] = (CHAR) SE; if (tn_deb || debses || deblog) { if (opt == TELOPT_START_TLS && sub == 1) sprintf(tn_msg,"TELNET SENT SB %s FOLLOWS IAC SE", TELOPT(opt) ); else if (opt == TELOPT_TTYPE && sub == 1) sprintf(tn_msg,"TELNET SENT SB %s SEND IAC SE", TELOPT(opt)); else if (opt == TELOPT_TTYPE && sub == 0) sprintf(tn_msg,"TELNET SENT SB %s IS %s IAC SE", TELOPT(opt),data); else if (opt == TELOPT_NEWENVIRON) { int i, quote; sprintf(tn_msg,"TELNET SENT SB %s %s", TELOPT(TELOPT_NEWENVIRON), sub == TELQUAL_SEND ? "SEND" : sub == TELQUAL_IS ? "IS" : sub == TELQUAL_INFO ?"INFO" : "UNKNOWN" ); for (i = 0, quote = 0; i < len; i++) { if (quote) { sprintf(hexbuf,"%02x",data[i]); strcat(tn_msg,hexbuf); quote = 0; } else { switch (data[i]) { case TEL_ENV_USERVAR: strcat(tn_msg," USERVAR "); break; case TEL_ENV_VAR: strcat(tn_msg," VAR "); break; case TEL_ENV_VALUE: strcat(tn_msg," VALUE "); break; case TEL_ENV_ESC: strcat(tn_msg," ESC "); quote = 1; break; case IAC: strcat(tn_msg," IAC "); break; default: sprintf(hexbuf,"%c",data[i]); strcat(tn_msg,hexbuf); } } } strcat(tn_msg," IAC SE"); } else sprintf(tn_msg, "TELNET SENT SB %s %02x <data> IAC SE", TELOPT(opt), sub ); debug(F101,tn_msg,"",opt); if (tn_deb || debses) tn_debug(tn_msg); } if (ttol(buf,6+len) < 6+len) return(-1); return(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -