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

📄 telnet.cpp

📁 vt100终端仿真程序
💻 CPP
字号:
// Telnet.cpp: implementation of the CTelnet class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "vt100.h"
#include "Telnet.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define NOP		241
#define SB		250
#define WILL	251
#define WONT	252
#define DO		253
#define DONT	254

#define STRLEN 10

#define WM_DATAARRIVAL (WM_USER+1)
#define WM_COMMERROR	(WM_USER+2)



extern HWND hOutWnd;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTelnet::CTelnet()
{
}

CTelnet::~CTelnet()
{
}

UINT CTelnet::RcvThread(LPVOID pParam)
{
	int iRetCode,iLoop;
	char szTemp[100];

	CCfgFile csCfgFile;
	int iAutoLogon;

	CCommunicate *pMyClass=(CCommunicate *)pParam;
	
	pMyClass->m_blIsRcvThreadActive=TRUE;
	((CTelnet *)pMyClass)->m_blNeg=TRUE;

	iAutoLogon=csCfgFile.GetCfgItemInt(LOGONMODE);
	if(iAutoLogon==0)
		((CTelnet *)pMyClass)->m_blLogonOk=TRUE; 
	else
		((CTelnet *)pMyClass)->m_blLogonOk=FALSE; 

	iLoop=1;
	while(iLoop==1){
		memset(pMyClass->m_szDataBuf,0,sizeof(pMyClass->m_szDataBuf));
		iRetCode=recv(((CTelnet *)pMyClass)->ServerAddrSocket,pMyClass->m_szDataBuf,BUFLEN,0);
		pMyClass->m_dwLen=iRetCode;

		switch(iRetCode){
		case 0:		//socket is closed
			sprintf(szTemp,"Socket is closeed!");
			csLog.WriteLog(0,0,szTemp,0); 
			PostMessage(pMyClass->m_pOwnerWnd,WM_COMMERROR,0,9);
			iLoop=0;
			break;							
		
		case SOCKET_ERROR: //error
			//data is arrived
			iRetCode=WSAGetLastError();
			if(iRetCode!=10053){
				sprintf(szTemp,"Rcv Error,error code=%d",iRetCode);
				csLog.WriteLog(0,0,szTemp,0); 
				PostMessage(pMyClass->m_pOwnerWnd,WM_COMMERROR,0,2);
			}
			iLoop=0;
			break;				

		default:
			csLog.WriteLog(0,0,pMyClass->m_szDataBuf,pMyClass->m_dwLen); 

			pMyClass->m_dwOffset=0;				
			if(((CTelnet *)pMyClass)->m_blNeg){
				iRetCode=((CTelnet *)pMyClass)->Negotiate();
				if(iRetCode!=0){
					sprintf(szTemp,"Rcv Error,neg error");
					csLog.WriteLog(0,0,szTemp,0); 
					PostMessage(pMyClass->m_pOwnerWnd,WM_COMMERROR,0,1);
					iLoop=0;
					break;
				}					
			}

			//for test mic telnet server
//			if(pMyClass->m_szDataBuf[pMyClass->m_dwOffset]=='\x0ff')
//				((CTelnet *)pMyClass)->Negotiate();
			// test end

			if(pMyClass->m_dwOffset!=pMyClass->m_dwLen) { 
				SendMessage(pMyClass->m_pOwnerWnd,WM_DATAARRIVAL,0,0);
			}

			break;
		}
	}
	
	pMyClass->m_blIsRcvThreadActive=FALSE;

	return 0;
}

UINT CTelnet::SndThread(LPVOID pParam)
{
	HANDLE hEventGroup[3];
	int iRetCode,iLoop;

	CCommunicate *pMyClass=(CCommunicate *)pParam;

	memset(hEventGroup,0,sizeof(HANDLE)*3);
	hEventGroup[0]=pMyClass->m_hSendDataEvent;
	hEventGroup[1]=pMyClass->m_hStopEvent;
	
	pMyClass->m_blIsSndThreadActive=TRUE;

	iLoop=1;
	while(iLoop==1){
		iRetCode=WaitForMultipleObjects(2,hEventGroup,FALSE,-1);
	
		switch(iRetCode){
		case WAIT_OBJECT_0:	//Send data request
			ResetEvent(hEventGroup[0]);
			iRetCode=send(((CTelnet *)pMyClass)->ServerAddrSocket,pMyClass->m_szSendBuf,pMyClass->m_dwSendLen,0); 
			if(iRetCode<=0){
				PostMessage(pMyClass->m_pOwnerWnd,WM_COMMERROR,2,1);
				iLoop=0;
			}
			break;
		
		case WAIT_OBJECT_0+1: //stop event
			ResetEvent(hEventGroup[1]);
			iLoop=0;
			break;

		default:
			PostMessage(pMyClass->m_pOwnerWnd,WM_COMMERROR,2,0);
			iLoop=0;
			break;
		}	
	}

	pMyClass->m_blIsSndThreadActive=FALSE;

	return 0;
}

int CTelnet::Negotiate()
{
	char szSendBuf[240];
	int iRetCode,iLoop;
	BYTE bCmdType;
//	BOOL blNeg;

	int iCmdOpt[]={	2,1,0,1,0,0,0,0,	//
				0,0,0,0,0,0,0,0,	//
				0,0,0,0,0,0,0,0,	//
				3,0,0,0,0,0,0,4,	//3:echo 4:width
				0,0,0,0,0,0,0,0		//
				};

	BOOL blOk;
	
	iLoop=0;
	blOk=TRUE;
	memset(szSendBuf,0,sizeof(szSendBuf));
	while(1){
		if(m_szDataBuf[m_dwOffset]!='\x0ff'){	//cmd header
			if(m_blLogonOk==FALSE){
				if(LogOn()!=0)
					blOk=FALSE;
			}
			else
				m_blNeg=FALSE;

			break;
		}

		if(m_szDataBuf[m_dwOffset+1]!='\x0f0'){	//3 byte cmd
			strncpy(szSendBuf+iLoop,m_szDataBuf+m_dwOffset,3);
			bCmdType=(BYTE)szSendBuf[iLoop+1];
			switch(iCmdOpt[szSendBuf[iLoop+2]]){
			case 0:
				if(bCmdType==WILL)
					szSendBuf[iLoop+1]=(char)DONT;
				else if(bCmdType==DO)
					szSendBuf[iLoop+1]=(char)WONT;
				else{
					m_dwOffset+=3;
					break;
				}
				iLoop+=3;
				m_dwOffset+=3;
						
				break;

			case 1:
				if(bCmdType==WILL)
					szSendBuf[iLoop+1]=(char)DO;
				else if(bCmdType==DO)
					szSendBuf[iLoop+1]=(char)WONT;
				else{
					m_dwOffset+=3;
					break;					
				}
				iLoop+=3;
				m_dwOffset+=3;
	
				break;

			case 2:
				if(bCmdType==WILL)
					szSendBuf[iLoop+1]=(char)DO;
				else if(bCmdType==DO)
					szSendBuf[iLoop+1]=(char)WILL;
				else{
					m_dwOffset+=3;
					break;
				}
				iLoop+=3;
				m_dwOffset+=3;

				break;


			case 3: //terminal type

//				{
//					TCHAR wsBuf[32];
//					swprintf(wsBuf,TEXT("bCmdType is : %d"),bCmdType);
// 					::MessageBox(hOutWnd,wsBuf,TEXT("Title"),MB_OK);
//				}

				if(bCmdType==DO){

					szSendBuf[iLoop+1]=(char)WILL;
					iLoop+=3;
					m_dwOffset+=3;
				}
				else if(bCmdType==WILL){
					szSendBuf[iLoop+1]=(char)DONT;
					iLoop+=3;
					m_dwOffset+=3;							
				}
				else if(bCmdType==SB){
					szSendBuf[iLoop+3]='\x0';
					strcpy(szSendBuf+iLoop+4,"vt100");						
					iLoop+=9;						
					m_dwOffset+=4;
				}
				else if(bCmdType==NOP){
					Trans(m_szDataBuf+m_dwOffset+3,szSendBuf+3);	
					szSendBuf[iLoop+13]=m_iTermNo+0x30;
					szSendBuf[iLoop+14]=0;
					iLoop+=14;						
					m_dwOffset+=13;
//					::MessageBox(hOutWnd,TEXT("CASE 3 NOP"),TEXT("Title"),MB_OK);
					/*
					szSendBuf[iLoop+3]='\x0';
//					strcpy(szSendBuf+iLoop+4,"vt100");	
					szSendBuf[iLoop+2]='\x31';
					iLoop+=3;						
					m_dwOffset+=4;
					*/
				}
				else{
					m_dwOffset+=3;
					break;						
				}

				break;

			case 4: //display attribute
				if(bCmdType==DO){
					szSendBuf[iLoop+1]=(char)WILL;
					szSendBuf[iLoop+3]='\x0ff';
					szSendBuf[iLoop+4]=(char)SB;
					szSendBuf[iLoop+5]='\x1f';
					szSendBuf[iLoop+6]=0;
					szSendBuf[iLoop+7]=80;
					szSendBuf[iLoop+8]=0;
					szSendBuf[iLoop+9]=24;
					szSendBuf[iLoop+10]='\x0ff';
					szSendBuf[iLoop+11]='\x0f0';
					iLoop+=12;
					m_dwOffset+=3;
				}
				else{
					m_dwOffset+=3;
					break;												
				}

				break;
			}
		}
		else{
			strncpy(szSendBuf+iLoop,m_szDataBuf+m_dwOffset,2);
			iLoop+=2;
			m_dwOffset+=2;
		}

		if((m_dwOffset==m_dwLen||m_blNeg==FALSE)&&m_dwOffset!=0&&iLoop!=0){
			//send to host	
			iRetCode=send(ServerAddrSocket,szSendBuf,iLoop,0); 
			if(iRetCode<=0){
				csLog.WriteLog(1,0,"Snd Error on neg",0); 
				blOk=FALSE;
			}
			else
				csLog.WriteLog(1,0,szSendBuf,iRetCode); 

			break;
		}
	}

	if(!blOk){  //error
		//send error msg
		return 1;
	}

	return 0;
}

int CTelnet::LogOn()
{
	TCHAR wszTemp[50];
	int iLoop,iLen,iRet;
	static int iStep;
	CCfgFile csCfgFile;

#ifdef UNICODE
	char szTemp[50];
#endif

	m_blLogonOk=TRUE;
	for(iLoop=0;iLoop<(int)m_dwLen;iLoop++){
		if(m_szDataBuf[iLoop]==':')
			break;
	}

	if(m_szDataBuf[iLoop]==':'&&iStep==0){
		iLen=csCfgFile.GetCfgItemStr(USERID,wszTemp,20); 
		if(iLen<=0){
			return 0;
		}
		
		wszTemp[iLen]=13;
		iLen++;

#ifdef UNICODE
		wcstombs(szTemp,wszTemp,iLen);
		iRet=send(ServerAddrSocket,szTemp,iLen,0);
#else
		iRet=send(ServerAddrSocket,wszTemp,iLen,0);
#endif
		
		if(iRet<=0){
			return 1;
		}		
		else{
			m_dwOffset=m_dwLen;
			iStep++;
			m_blLogonOk=FALSE;
			return 0;
		}
	}

	if(m_szDataBuf[iLoop]==':'&&iStep==1){
		iLen=csCfgFile.GetCfgItemStr(PASSWORD,wszTemp,16); 
		for(iLoop=0;iLoop<iLen;iLoop++){
			wszTemp[iLoop]=~wszTemp[iLoop];
		}
		
		wszTemp[iLen]=13;
		iLen++;

#ifdef UNICODE
		wcstombs(szTemp,wszTemp,iLen);
		iRet=send(ServerAddrSocket,szTemp,iLen,0);
#else
		iRet=send(ServerAddrSocket,wszTemp,iLen,0);
#endif
		
		if(iRet<=0){
			return 1;
		} 		
		else{
			m_dwOffset=m_dwLen;
			iStep=0;
			return 0;
		}
	}
	else{
		m_dwOffset=m_dwLen;
		m_blLogonOk=FALSE;
	}

	return 0;
}

int CTelnet::Trans(char *pszSrc, char *pszDes)
{
	int aa[10]={11,13,17,19,23,29,31,37,41,43};
	int i,j;
	int p;

	int iSum=0;

	for (i=0;i<STRLEN;i++)
	{
		for (j=0;j<STRLEN;j++)
		{
			p=(i+j)%10;
			iSum+=(pszSrc[j]%10)*aa[p];
		}
		pszDes[i]=iSum%10+0x30;
	}

	return 0;

}


⌨️ 快捷键说明

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