BOOL SendKeepAlive(PHGlobal *phglobal, int opCode) { DATA_KEEPALIVE data; blf_ctx blf; char p1[KEEPALIVE_PACKET_LEN],p2[KEEPALIVE_PACKET_LEN]; memset(&data,0,sizeof(data)); data.lChatID = phglobal->nChatID; data.lID = phglobal->nStartID; data.lOpCode = opCode; data.lSum = 0 - (data.lID + data.lOpCode); data.lReserved = 0; if (!phglobal->bTcpUpdateSuccessed) return FALSE; LOG(1) ("SendKeepAlive() %d\n",opCode); InitBlowfish(&blf, (unsigned char*)phglobal->szChallenge,phglobal->nChallengeLen); memcpy(p1,&data,KEEPALIVE_PACKET_LEN); memcpy(p2,&data,KEEPALIVE_PACKET_LEN); Blowfish_EnCode(&blf, p1+4,p2+4,KEEPALIVE_PACKET_LEN-4); phSend(phglobal->m_udpsocket, p2, KEEPALIVE_PACKET_LEN,0); //RecvKeepaliveResponse(); return TRUE; }
void CBlowfish(unsigned char* keybuf,unsigned int len) { unsigned int i; blowfish_keylen=len; if (blowfish_keylen>MAXKEYBYTES) blowfish_keylen=MAXKEYBYTES; for (i=0;i<=blowfish_keylen;i++) blowfish_key[i]=keybuf[i]; InitBlowfish(); }
int RecvKeepaliveResponse(PHGlobal *phglobal) { char temp[100]; DATA_KEEPALIVE_EXT rdata; DATA_KEEPALIVE data; blf_ctx blf; char p1[KEEPALIVE_PACKET_LEN],p2[KEEPALIVE_PACKET_LEN]; if (!phglobal->bTcpUpdateSuccessed) return errorOccupyReconnect; //prevent the thread to be suspended while waiting for data if (phDataReadable(phglobal->m_udpsocket, 0)<=0) { return okNoData; } //DATA_KEEPALIVE data; //if (m_udpsocket.Receive(&data,sizeof(DATA_KEEPALIVE),0)<=0) return FALSE; if (phReceive(phglobal->m_udpsocket, temp,sizeof(temp),0)<=0) return okNoData; memcpy(&rdata, temp, sizeof(DATA_KEEPALIVE_EXT)); data = rdata.keepalive; InitBlowfish(&blf, (unsigned char*)phglobal->szChallenge,phglobal->nChallengeLen); memcpy(p1,&data,KEEPALIVE_PACKET_LEN); memcpy(p2,&data,KEEPALIVE_PACKET_LEN); Blowfish_DeCode(&blf, p1+4,p2+4,KEEPALIVE_PACKET_LEN-4); memcpy(&data,p2,KEEPALIVE_PACKET_LEN); phglobal->nStartID = data.lID + 1; LOG(1) (("RecvKeepaliveResponse() Data comes, OPCODE:%d\n"),data.lOpCode); if (data.lID - phglobal->nLastResponseID > 3 && phglobal->nLastResponseID != -1) { return errorOccupyReconnect; } phglobal->nLastResponseID = data.lID; phglobal->tmLastResponse = time(0); phglobal->ip = rdata.ip; if (data.lOpCode == UDP_OPCODE_UPDATE_ERROR) return okServerER; //if (data.lOpCode == UDP_OPCODE_LOGOUT) return okNormal; return okKeepAliveRecved; }