📄 upnodelogin.cpp
字号:
/********************************************************************** FileName : NodeLogin.cpp Description : 上节点登录模块 Version : 1.0 Date : 2003年6月16日 Author : 刘荣辉 Other : 发送Login包,接收LoginRsp包 mode=0,收发模式;mode=1,只收模式;mode=2,只发模式***********************************************************************/#include "../include/UpNode.h"//int SMGPLogin(int mode); //SMGP登录模块//int CMPPLogin(int mode); //CMPP登录模块int CUpNode::NodeLogin(int mode){ int RetCode; if(Protocol==0) { RetCode=CMPPLogin(mode); } else if(Protocol==1) { RetCode=SMGPLogin(mode); } return RetCode;}//=====================================================================================//=====================================SMGP登录模块====================================//=====================================================================================int CUpNode::SMGPLogin(int mode) //SMGP登录模块{ int RetCode; CFunc ShareFunc; //共享子函数集 SMGP_Login m_pLogin; memset(&m_pLogin,0,sizeof(SMGP_Login)); //--------------------------形成Login包----------------------------- m_pLogin.msgHead.nPacketLength = htonl(sizeof(SMGP_Login)); m_pLogin.msgHead.nRequestId = htonl(REQUEST_ID_LOGIN); m_pLogin.msgHead.nSequenceId = htonl(GetSequence()); //Login消息体 memcpy(m_pLogin.sUser,SP_Id,strlen(SP_Id)); //企业代码 //认证码生成 MD5_CTX md5T; unsigned char digest[16]; unsigned char* pCur; unsigned char sTmp[128]; //a.源地址 memset(sTmp,0,sizeof(sTmp)); pCur=sTmp; memcpy(pCur,SP_Id,strlen(SP_Id)); pCur+=strlen(SP_Id); pCur+=7; //b.密码 memcpy(pCur,Secret,strlen(Secret)); pCur+=strlen(Secret); char sTime[11]; ShareFunc.setTimeStamp(sTime); //时间戳,如"0618172219" memcpy(pCur,sTime,strlen(sTime)); pCur+=10; //printf("\n SP_Id=[%s],Secret=[%s],sTime=[%s]",SP_Id,Secret,sTime); //printf("\nsTmp="); //for(unsigned int i=0;i<20+strlen(SP_Id)+strlen(Secret);i++) // printf("%c,",sTmp[i]); //printf("\n"); //生成MD5码 md5T.MD5Update ((unsigned char*)sTmp, 17+strlen(SP_Id)+strlen(Secret)); md5T.MD5Final (digest); memcpy(m_pLogin.sAuthenMD5,digest,16); m_pLogin.nLoginMode = mode; //登录类型 m_pLogin.nTimeStamp = htonl(atoi(sTime)); //时间戳 m_pLogin.nVersion = SMGP_CLIENT_VERSION; //版本号 /*/-----------------debug---------------------- printf("\n===========checking SMGP_Login Pack============= \n"); unsigned char *ptmp; ptmp=(unsigned char *)&m_pLogin; printf("\n sizeof(SMGP_Login)=[%d]\n",sizeof(SMGP_Login)); for(int i=0;i<(int)sizeof(SMGP_Login);i++) { printf("%02x ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); for(int i=0;i<(int)sizeof(SMGP_Login);i++) { printf("%d ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); //--------------------debug end-------------------*/ RetCode = TcpSock.Writen(&m_pLogin,sizeof(SMGP_Login)); //发送Login包 if(RetCode != sizeof(SMGP_Login)) { State = 1; //节点状态为正在连接 printf("\n SMGPLogin: Sending Login Pack error! \n"); return RetCode; } SMGP_LoginResp strLoginResp; //RetCode = TcpSock.RecvPack2(&strLoginResp,sizeof(SMGP_LoginResp)); //接收Login_rsp RetCode = TcpSock.RecvPack2(&strLoginResp,33); //接收Login_rsp,sizeof(SMGP_LoginResp)!=33 if(RetCode) { State = 1; //节点状态为正在连接 printf("\n SMGPLogin: RecvPack2() error when waiting for SMGP_LoginResp.\n"); return RetCode; //错误码为负数 } /*/-----------------debug---------------------- printf("\n SMGPLogin: Got Login_rsp! \n"); printf("\n===========checking Login_Rsp Pack============= \n"); //unsigned char *ptmp; ptmp=(unsigned char *)&strLoginResp; for(int i=0;i<33;i++) { printf("%02x ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); for(int i=0;i<33;i++) { printf("%d ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); //--------------------debug end-------------------*/ //------应答包字节顺序转换------- strLoginResp.msgHead.nPacketLength = ntohl(strLoginResp.msgHead.nPacketLength); strLoginResp.msgHead.nRequestId = ntohl(strLoginResp.msgHead.nRequestId); strLoginResp.msgHead.nSequenceId = ntohl(strLoginResp.msgHead.nSequenceId); strLoginResp.nResult = ntohl(strLoginResp.nResult); /*/-----------------debug---------------------- printf("\n=========checking Login_Rsp Pack after transformation========= \n"); //unsigned char *ptmp; ptmp=(unsigned char *)&strLoginResp; for(int i=0;i<33;i++) { printf("%02x ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); for(int i=0;i<33;i++) { printf("%d ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); //--------------------debug end-------------------*/ if(strLoginResp.msgHead.nRequestId!=REQUEST_ID_LOGIN_RESP) //收到的包不是Login_rsp包 { State = 1; printf("\n [UpNodeLogin.cpp]: Error! nRequestId!=REQUEST_ID_LOGIN_RESP.\n"); return -3; } if(strLoginResp.nResult) { State = 1; return (int)strLoginResp.nResult ; } return 0;}//=====================================================================================//=====================================CMPP登录模块====================================//=====================================================================================int CUpNode::CMPPLogin(int mode) //CMPP登录模块{ int RetCode; CFunc ShareFunc; //共享子函数集 CMPP_Connect m_pLogin; memset(&m_pLogin,0,sizeof(CMPP_Connect)); //--------------------------形成Login包----------------------------- m_pLogin.Head.Total_Length = htonl(sizeof(CMPP_Connect)); m_pLogin.Head.Command_Id = htonl(CMPP_CONNECT); m_pLogin.Head.Sequence_Id = htonl(GetSequence()); //Login消息体 memcpy(m_pLogin.Source_Addr,SP_Id,strlen(SP_Id)); //企业代码 //-------MD5认证码生成---------- MD5_CTX md5T; unsigned char digest[16]; unsigned char* pCur; unsigned char sTmp[128]; //a.源地址 memset(sTmp,0,sizeof(sTmp)); pCur=sTmp; memcpy(pCur,m_pLogin.Source_Addr,strlen(SP_Id)); pCur+=strlen(SP_Id); pCur+=9; //9字节的0x00 //b.密码 memcpy(pCur,Secret,strlen(Secret)); pCur+=strlen(Secret); char sTime[11]; ShareFunc.setTimeStamp(sTime); //时间戳,如"0618172219" memcpy(pCur,sTime,strlen(sTime)); //pCur+=10; /*/-------------debug------------- printf("\n SP_Id=[%s],Secret=[%s],sTime=[%s]",SP_Id,Secret,sTime); printf("\nsTmp="); for(unsigned int i=0;i<20+strlen(SP_Id)+strlen(Secret);i++) printf("%c,",sTmp[i]); printf("\n"); //-------------debug end-------------*/ //生成MD5码 md5T.MD5Update ((unsigned char*)sTmp, 19+strlen(SP_Id)+strlen(Secret)); //19=9+10,9为0x00,10为timestamp md5T.MD5Final (digest); memcpy(m_pLogin.AuthenticatorSource,digest,16); //------------------------------- m_pLogin.Version = CMPPVersion; //版本号 m_pLogin.Timestamp = htonl(atoi(sTime)); //时间戳 /*/-----------------debug---------------------- printf("\n===========checking CMPP Login Pack============= \n"); unsigned char *ptmp; ptmp=(unsigned char *)&m_pLogin; for(int i=0;i<(int)sizeof(CMPP_Connect);i++) { printf("%02x ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); for(int i=0;i<(int)sizeof(CMPP_Connect);i++) { printf("%d ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); //--------------------debug end-------------------*/ //--------------------------------------------------------- RetCode = TcpSock.Writen(&m_pLogin,sizeof(CMPP_Connect)); //发送Login包 if(RetCode != sizeof(CMPP_Connect)) { State = 1; //节点状态为正在连接 printf("\n CMPPLogin: Sending Login Pack error! \n"); return RetCode; } #ifdef DEBUG printf("\n CMPPLogin: Login Pack is sent successfully! \n"); #endif CMPP_Connect_Resp strLoginResp; RetCode = TcpSock.RecvPack2(&strLoginResp,sizeof(CMPP_Connect_Resp)); //接收Login_rsp if(RetCode) { State = 1; //节点状态为正在连接 printf("\n CMPPLogin: RecvPack2() error when waiting for CMPP_Connect_Resp.\n"); return RetCode; } /*/-----------------debug---------------------- printf("\n CMPPLogin: Got Login_rsp! \n"); printf("\n===========checking Login_Rsp Pack============= \n"); //unsigned char *ptmp; ptmp=(unsigned char *)&strLoginResp; for(int i=0;i<33;i++) { printf("%02x ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); for(int i=0;i<30;i++) { printf("%d ",*(ptmp+i)); if(((i+1)%4)==0) printf(" "); if((i+1)==12) printf("\n"); } printf("\n=========================================== \n"); //--------------------debug end-------------------*/ //------应答包字节顺序转换------- strLoginResp.Head.Total_Length = ntohl(strLoginResp.Head.Total_Length); strLoginResp.Head.Command_Id = ntohl(strLoginResp.Head.Command_Id); strLoginResp.Head.Sequence_Id = ntohl(strLoginResp.Head.Sequence_Id); if(strLoginResp.Head.Command_Id!=CMPP_CONNECT_RESP) //收到的包不是Login_rsp包 { State = 1; return -3; } if(strLoginResp.Status) { State = 1; return (int)strLoginResp.Status ; } //printf("\n CMPPLogin: Got good Connect_Rsp.\n"); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -