ConnectionResult CGenericServer::FirstPhaseConnection(const CString& key,const char* local_ip,
										  char* buff, int buf_len,int& reply_length)
{
	CString s_address;
	int s_port;
	CDataBuffer request;

	//request line
	CHttpRequest http_request(GET_M,DIFFIE_HELLMAN_CLIENT_HELLO,HTTP_1_0,EMPTY_STRING);
	//headers
	http_request.AddHeader(NETWORK_NAME_HEADER,_network_name);
	http_request.AddHeader(DATA_PORT_HEADER,_data_sock.GetLocalPort());
	http_request.AddHeader(KEEPALIVE_PORT_HEADER,_thread->GetHeartBeatPort());
	http_request.AddHeader(ADDRESS_HEADER,local_ip);
	//end
	http_request.Finalize(request);

	CDataBuffer::Encrypt(request.GetBuffer(),request.GetLength(),&key);


	START_TRY

		GetLog()->SetConsoleColor(YELLOW);
		GetLog()->Log(LOG_LEVEL_INFO,"[%s] [Send] Client Hello [%s:%d --> %s:%d]",GetUserName().GetBuffer(),
						local_ip,_data_sock.GetLocalPort(),_eib_address.GetBuffer(),_eib_port);
		_data_sock.SendTo(request.GetBuffer(),request.GetLength(),_eib_address,_eib_port);
		reply_length = _data_sock.RecvFrom(buff,buf_len,s_address,s_port,5000);

	END_TRY_START_CATCH(ex)
		GetLog()->SetConsoleColor(RED);
		GetLog()->Log(LOG_LEVEL_ERROR,"[%s] Cannot connect to eib server: %s",GetUserName().GetBuffer(),ex.what());
 		return STATUS_INRERNAL_ERR;
	END_TRY_START_CATCH_SOCKET(e)
		GetLog()->SetConsoleColor(RED);
		GetLog()->Log(LOG_LEVEL_ERROR,"[%s] Socket Error: Reason: %s",GetUserName().GetBuffer(),e.what());
 		return STATUS_INRERNAL_ERR;
	END_TRY_START_CATCH_ANY
		GetLog()->SetConsoleColor(RED);
		GetLog()->Log(LOG_LEVEL_ERROR,"[%s] Cannot connect to eib server... Unknown Exception.",GetUserName().GetBuffer());
		return STATUS_INRERNAL_ERR;
	END_CATCH

	if (reply_length > 0){
		return STATUS_CONN_OK;
	}
	//no reply from EIB...
	GetLog()->SetConsoleColor(RED);
	GetLog()->Log(LOG_LEVEL_ERROR,"No reply from EIB Server. maybe EIBServer is down?");
	return STATUS_NO_REPLY;
}
Beispiel #2
0
	void SetAuthUser(unsigned int nUserId) {
		assert(m_nUserId==0);
		m_nUserId = nUserId;
		CDataBuffer<100> sbuf;
		sbuf.PutValue<unsigned int>(0);
		sbuf.PutValue<unsigned short>(0);
		sbuf.PutValue(m_nSeq);
		pLoop->PushMsg(SGCMDCODE_CONNECT, nUserId, sbuf.GetBuffer(), sbuf.GetLength());
	}
Beispiel #3
0
	void OnConnect(unsigned int nIP, unsigned short nPort) {
		m_bActive = TRUE;
		m_nIP = nIP;
		m_nPort = nPort;
		CDataBuffer<100> buf;
		unsigned char aSalt[10];
		buf.PutValue<unsigned short>(SGCMDCODE_LOGIN_SEED);
		memset(aSalt, 0xf3, sizeof(aSalt));
		buf.PutArray(aSalt, sizeof(aSalt));
		SendData(buf.GetBuffer(), buf.GetLength());
	}
Beispiel #4
0
void CDataBuffer::Set(const CDataBuffer& data)
{
	Clear();
	Add((char*)data.GetBuffer(),data.GetLength());
}
Beispiel #5
0
	void OnData(DWORD nSize, LPVOID pData) {
		if(!m_bActive) return;

		if(sizeof(m_DataBuf)-m_dwDataBufSize<nSize) {
			Disconnect();
			return;
		}

		memcpy(m_DataBuf+m_dwDataBufSize, pData, nSize);
		m_dwDataBufSize += nSize;

		for(;;) {
			WORD len;
			if(m_dwDataBufSize<sizeof(len)) break;
			len = *((WORD*)m_DataBuf);
			if(m_dwDataBufSize<sizeof(len)+len) break;

			WORD code;
			if(sizeof(code)>len) {
				Disconnect();
				return;
			}
			code = *((WORD*)(m_DataBuf+sizeof(len)));

			switch(code) {
			case SGCMDCODE_LOGIN:
				if(m_nUserId) {
					Disconnect();
					return;
				}

				{
					unsigned int nUserId;
					const char* pUserName;
					const char* pPassword;
					CDataReader buf(m_DataBuf+sizeof(len)+sizeof(code), len-sizeof(code));
					buf.GetString(pUserName);
					buf.GetString(pPassword);
					nUserId = (unsigned int)atoi(pUserName);
					int ret = -1;
					if(strcmp(pPassword, "password")==0) {
						ret = 0;
						SetAuthUser(nUserId);
					}

					CDataBuffer<100> sbuf;
					sbuf.PutValue<unsigned short>(SGCMDCODE_LOGIN_RETURN);
					sbuf.PutValue(ret);
					SendData(sbuf.GetBuffer(), sbuf.GetLength());
				}
				break;
			default:
				if(!m_nUserId) {
					Disconnect();
					return;
				}
				pLoop->PushMsg(SGCMDCODE_USERDATA, m_nUserId, m_DataBuf+sizeof(len), len);
				break;
			}

			memmove(m_DataBuf, &m_DataBuf[sizeof(len)+len], m_dwDataBufSize-sizeof(len)-len);
			m_dwDataBufSize -= sizeof(len) + len;
		}
	}