Beispiel #1
0
void CG2Node::OnPacket(G2Packet* pPacket)
{
	//qDebug() << "Got packet " << pPacket->GetType() << pPacket->ToHex() << pPacket->ToASCII();

	//try
	//{
        if( !Network.RoutePacket(pPacket) )
        {

            if( pPacket->IsType("PI") )
            {
                OnPing(pPacket);
            }
            else if( pPacket->IsType("PO") )
            {
                OnPong(pPacket);
            }
            else if( pPacket->IsType("LNI") )
            {
                OnLNI(pPacket);
            }
            else if( pPacket->IsType("KHL") )
            {
                OnKHL(pPacket);
            }
            else if( pPacket->IsType("QHT") )
            {
                //OnQHT(pPacket);
            }
            else if( pPacket->IsType("Q2") )
            {
				OnQuery(pPacket);
            }
			else if( pPacket->IsType("QKR") )
			{
				OnQKR(pPacket);
			}
            else if( pPacket->IsType("QKA") )
            {
                OnQKA(pPacket);
            }
            else if( pPacket->IsType("QA") )
            {
                OnQA(pPacket);
            }
            else if( pPacket->IsType("QH2") )
            {
                OnQH2(pPacket);
            }
            else
            {
                qDebug() << "Unknown packet " << pPacket->GetType();
            }
        }
	/*}
    catch(...)
    {
        qDebug() << "Packet error in child packets";
	}*/
}
Beispiel #2
0
// Translates message and does the appropriate task for message handled by
// the NDK.
void CNDKServer::TranslateMessage(long lUserId, CNDKMessage& message)
{
	switch (message.GetId())
	{
	case NDKPingServer:
		SendMessageToUser(lUserId, message);
		break;

	case NDKPingClient:
		{
			long lNbMilliseconds = 0;
			message.GetAt(0, lNbMilliseconds);

			OnPing(lUserId, ::GetTickCount() - lNbMilliseconds);
		}
		break;

	case NDKClientDisconnect:
		DisconnectUser(lUserId, NDKServer_ClientCloseConnection);
		break;

	default:
		OnMessage(lUserId, message);
		break;
	}
}
Beispiel #3
0
bool CNatThread::ProcessPacket(const BYTE* packet, UINT size, uint32 ip, uint16 port)
{
	uint8 opcode=packet[4];
	const uchar * realdata=packet+5;
	UINT realsize=size-5;
	
	CSingleLock locker(&m_Mutex, TRUE);
	
// modiefied by robert, 2010-08-03
// beta版屏蔽try,以求暴露更多问题
#ifndef _BETA
	try
	{
#endif

	switch(opcode)
	{
	case OP_NAT_REPING:
		OnReping(ip, port, realdata, realsize);
		break;
	case OP_NAT_REGISTER:

		// VC-SearchDream[2007-06-15]: Add Login Support
		if (realsize >= 2 && !m_bRegister && ip==m_dwSvrIp && port==m_wSvrPort )
		{
			if (realsize >= 6)
			{
				WORD	port	= *(WORD*)(realdata);
				DWORD	ip		= *(DWORD*)(realdata + 2);

				if (ip != m_dwSvrIp || port != m_wSvrPort)
				{
					m_dwSvrIp = ip;
					m_wSvrPort = port;
					m_bRegister	= false;
					RegisterMe(m_dwSvrIp,m_wSvrPort);
					m_dwRegAttemptTimes = 1;
					m_dwRegNextAttempInterval = INIT_ATTEMPT_INTERVAL*2;
					break;
				}
			}
			else
			{
				WORD  port = *(WORD*)(realdata);

				if (port != m_wSvrPort)
				{
					m_wSvrPort	= port;
					m_bRegister	= false;
					RegisterMe(m_dwSvrIp,m_wSvrPort);
					m_dwRegAttemptTimes = 1;
					m_dwRegNextAttempInterval = INIT_ATTEMPT_INTERVAL*2;
					T_TRACE("new server port : %d.", ntohs(port));
					break;
				}
			}
		}
	
		m_dwSvrRetTime = time(NULL);

		if(!m_bRegister)
		{
			m_bRegister=true;
			m_dwSvrKeepalive = time(NULL);
			m_dwRegAttemptTimes = 0;
			m_dwRegNextAttempInterval = INIT_ATTEMPT_INTERVAL;
			T_TRACE("reset svr time in register");
			AddLogLine(false, _T("Registered to NAT traversal server\n"));
		}

		break;
	case OP_NAT_FAILED:
		{
/*
			if(realdata[0]==2)
			{
				m_bRegister = false;
				break;
			}
			else */if(realdata[0]==1)
			{
				realdata++;
				realsize--;
				int n=realsize/16;

				if(ProcessStrategyPacket(realdata, ip, port, packet+4, size-4))
					break;

				for(int i=0; i<n; i++)
				{
					TRACE(_T("failed conn request for %s\n"),UserHashToString(realdata));
					CUnconnSocket * us= FindUnconnectSocket(realdata);
					if(us) us->m_dwState |= NAT_E_NOPEER;
					realdata+=16;
				}
			}
		}
		break;
	//case OP_NAT_SYNC2:
		//if(ProcessStrategyPacket(realdata+6, ip, port, packet+4, size-4))
		//	break;
		//OnSync2(ip, port, realdata, realsize);
		//break;
	case OP_NAT_SYNC2:
	case OP_NAT_SYNC:
		OnSync(ip, port, realdata, realsize);
		break;
	case OP_NAT_PING:
		OnPing(ip, port, realdata, realsize);
		break;
	case OP_NAT_DATA:
		{
			if(size < 4) // ACK (+ DATA)
				break;

			DWORD ConnAck = 0;
			memcpy(&ConnAck, realdata, 4);
			realdata += 4;
			realsize -= 4;
			CNatSocket* source = FindNatSocket(ip, port, ConnAck);
			if( source == NULL )
			{
#ifdef _DEBUG_NAT
				TRACE("\n*** no nat sock to recv NAT_DATA. connack=%08x\n", ConnAck);
#endif
				uint32 uSequenceNr = PeekUInt32(realdata);
				if(uSequenceNr==1)
				{
					uchar pack[16];
					GetMyUserHash(pack);
					CNatSocket::SendPacket(ip, port, OP_NAT_REPING, pack, 16);
				}
				else 
				{
					CNatSocket::SendPacket(ip, port, OP_NAT_RST, NULL, 0);
				}
				break;
			}


			if(source->ProcessDataPacket(realdata, realsize))
			{
				//TRACE("source->ProcessDataPacket is OK. tag=%08x. Asyncsock=%08x\n", tag,source->GetParent());
				//source->OnReceive(0);
				source->m_Parent->TriggerEvent(FD_READ);
				return true;
			}
			else
			{
				T_TRACE("\nFailed to source->ProcessDataPacket. ConnAck=%08x\n\n", ConnAck);
			}
			break;
		}
	case OP_NAT_ACK:
		{
			DWORD ConnAck = 0;
			memcpy(&ConnAck, realdata, 4);
			realdata+=4;
			realsize-=4;
			CNatSocket* source = FindNatSocket(ip, port, ConnAck);
			if( source == NULL )
			{
#ifdef _DEBUG_NAT
				TRACE("\nsend RST for no nat sock to do NAT_ACK\n");
#endif
				CNatSocket::SendPacket(ip, port, OP_NAT_RST,0,0);
				break;
			}
			source->ProcessAckPacket(realdata, realsize);
			break;
		}
	case OP_NAT_RST:
		{
			break;
		}
	default:
		TRACE("Unknown command\n");
		break;
	}

// modiefied by robert, 2010-08-03
// beta版屏蔽try,以求暴露更多问题
#ifndef _BETA
	}catch(...)
		{
			TRACE("Exception: %s\n", __FUNCTION__);
		}
#endif

	return true;
}
Beispiel #4
0
void IrcClient::OnLine(RCString line) {
	smatch m;
	string sline = explicit_cast<string>(line);
	if (regex_search(sline, m, s_reMessage)) {
		String prefix = m[1];
		String c = m[3];
		String reply = m[2];
		string pars = m[4];
		vector<String> params;
		for (regex_iterator<string::const_iterator> it(pars.begin(), pars.end(), s_reParams), e; it!=e; ++it) {
			params.push_back((*it)[1].matched ? (*it)[1] : (*it)[2]);
		}
		if (!reply.empty()) {
			int nReply = atoi(reply);
			switch (nReply) {

			case RPL_CREATED:
				SendPendingData();
				ConnectionEstablished = true;
				OnCreatedConnection();
				break;
			case RPL_USERHOST:
				{
					smatch m;
					if (regex_search(pars, m, s_reUserhost)) {
						String host = m[1];
						OnUserHost(host);
					}
				}
				break;
			case RPL_WHOREPLY:
				if (params.size() > 7) {
					String channel = params.at(1).substr(1);
					String realname = params.at(7).Split("", 2).at(1).Trim();
					IrcUserInfo info = { params.at(2), params.at(3), params.at(4), params.at(5), realname };
					m_whoLists[channel].push_back(info);
				}
				break;
			case RPL_NAMREPLY: 
				{
					String channel = params.at(2).substr(1);
					vector<String> nicks = params.at(3).Split();
					m_nameList[channel].insert(nicks.begin(), nicks.end());
				}
				break;
			case RPL_ENDOFNAMES:
				{
					String channel = params.at(1).substr(1);
					OnNickNamesComplete(channel, m_nameList[channel]);
				}
				break;
			case RPL_ENDOFWHO:
				{
					String channel = params.at(1).substr(1);
					CWhoList::iterator it = m_whoLists.find(channel);
					if (it != m_whoLists.end()) {
						vector<IrcUserInfo> vec = it->second;
						m_whoLists.erase(it);
						OnUserListComplete(channel, vec);
					}
				}
				break;
			}
		} else if (!c.empty()) {
			if (c == "NOTICE") {
				if (params.size() > 0) {
					if (params[0] == "AUTH") {
						OnAuth();
					}
				}
			} else if (c == "PING") {
				String s1 = params.at(0), s2;
				if (params.size() >= 2)
					s2 = params.at(1);
				OnPing(s1, s2);
			}
		}
	} else
		Throw(E_FAIL);
}
Beispiel #5
0
BOOL CDCNeighbour::OnPacket(CDCPacket* pPacket)
{
	pPacket->SmartDump( &m_pHost, FALSE, FALSE, (DWORD_PTR)this );

	m_nInputCount++;
	m_tLastPacket = GetTickCount();

	if ( pPacket->m_nLength < 2  )
	{
		return OnPing();
	}
	else if ( *pPacket->m_pBuffer == '<' )
	{
		return OnChat( pPacket );
	}
	else if ( *pPacket->m_pBuffer != '$' )
	{
		return OnUnknown( pPacket );
	}

	if ( pPacket->Compare( _P("$Search ") ) )
	{
		return OnQuery( pPacket );
	}
	else if ( pPacket->Compare( _P("$To: ") ) )
	{
		return OnChatPrivate( pPacket );
	}
	else if ( pPacket->Compare( _P("$HubTopic ") ) )
	{
		return OnHubTopic( pPacket );
	}
	else if ( pPacket->Compare( _P("$HubName" ) ) )
	{
		return OnHubName( pPacket );
	}

	// Convert '|' to '\0' (make ASCIIZ)
	pPacket->m_pBuffer[ pPacket->m_nLength - 1 ] = 0;
	LPCSTR szCommand = (LPCSTR)pPacket->m_pBuffer;

	// Split off parameters
	LPSTR szParams = strchr( (LPSTR)pPacket->m_pBuffer, ' ' );
	if ( szParams )
	{
		*szParams++ = 0;
	}

	if ( strcmp( szCommand, "$MyINFO" ) == 0 )
	{
		return OnUserInfo( szParams );
	}
	else if ( strcmp( szCommand, "$Quit" ) == 0 )
	{
		return OnQuit( szParams );
	}
	else if ( strcmp( szCommand, "$Lock" ) == 0 )
	{
		return OnLock( szParams );
	}
	else if ( strcmp( szCommand, "$Supports" ) == 0 )
	{
		return OnSupports( szParams );
	}
	else if ( strcmp( szCommand, "$Hello" ) == 0 )
	{
		return OnHello( szParams );
	}
	else if ( strcmp( szCommand, "$OpList" ) == 0 )
	{
		return OnOpList( szParams );
	}
	else if ( strcmp( szCommand, "$ConnectToMe" ) == 0 )
	{
		return OnConnectToMe( szParams );
	}
	else if ( strcmp( szCommand, "$ForceMove " ) == 0 )
	{
		return OnForceMove( szParams );
	}
	else if ( strcmp( szCommand, "$ValidateDenide" ) == 0 )
	{
		return OnValidateDenide();
	}
	else if ( strcmp( szCommand, "$GetPass" ) == 0 )
	{
		return OnGetPass();
	}
	else if ( strcmp( szCommand, "$UserIP" ) == 0 )
	{
		return OnUserIP( szParams );
	}
	else if ( strcmp( szCommand, "$ZOn" ) == 0 )
	{
		return OnZOn();
	}
	else if ( strcmp( szCommand, "$RevConnectToMe" ) == 0 )
	{
		return OnRevConnectToMe( szParams );
	}

	return OnUnknown( pPacket );
}
Beispiel #6
0
inline void
Server::OnDatagramReceived(Client &&client,
                           void *data, size_t length)
{
  Header &header = *(Header *)data;
  if (length < sizeof(header))
    return;

  const uint16_t received_crc = FromBE16(header.crc);
  header.crc = 0;

  const uint16_t calculated_crc = UpdateCRC16CCITT(data, length, 0);
  if (received_crc != calculated_crc)
    return;

  client.key = FromBE64(header.key);

  const auto &ping = *(const PingPacket *)data;
  const auto &fix = *(const FixPacket *)data;
  const auto &traffic = *(const TrafficRequestPacket *)data;
  const auto &user_name = *(const UserNameRequestPacket *)data;
  const auto &wave = ((const WaveSubmitPacket *)data)->wave;
  const auto &thermal = ((const ThermalSubmitPacket *)data)->thermal;

  switch ((Type)FromBE16(header.type)) {
  case PING:
    if (length < sizeof(ping))
      return;

    OnPing(client, FromBE16(ping.id));
    break;

  case FIX:
    if (length < sizeof(fix))
      return;

    OnFix(client,
          ImportTimeMs(fix.time),
          fix.flags & ToBE32(FixPacket::FLAG_LOCATION)
          ? ImportGeoPoint(fix.location)
          : ::GeoPoint::Invalid(),
          fix.flags & ToBE32(FixPacket::FLAG_ALTITUDE)
          ? FromBE16(fix.altitude)
          : -1);
    break;

  case TRAFFIC_REQUEST:
    if (length < sizeof(traffic))
      return;

    OnTrafficRequest(client,
                     traffic.flags & ToBE32(TrafficRequestPacket::FLAG_NEAR));
    break;

  case USER_NAME_REQUEST:
    if (length < sizeof(user_name))
      return;

    OnUserNameRequest(client,
                      FromBE32(user_name.user_id));
    break;

  case WAVE_SUBMIT:
    if (length < sizeof(wave))
      return;

    OnWaveSubmit(client,
                 ImportTimeMs(wave.time),
                 ImportGeoPoint(wave.a), ImportGeoPoint(wave.b),
                 (int16_t)FromBE16(wave.bottom_altitude),
                 (int16_t)FromBE16(wave.top_altitude),
                 FromBE16(wave.lift) / 256.);
    break;

  case WAVE_REQUEST:
    OnWaveRequest(client);
    break;

  case THERMAL_SUBMIT:
    if (length < sizeof(thermal))
      return;

    OnThermalSubmit(client,
                    ImportTimeMs(thermal.time),
                    ImportGeoPoint(thermal.bottom_location),
                    (int16_t)FromBE16(thermal.bottom_altitude),
                    ImportGeoPoint(thermal.top_location),
                    (int16_t)FromBE16(thermal.top_altitude),
                    FromBE16(thermal.lift) / 256.);
    break;

  case THERMAL_REQUEST:
    OnThermalRequest(client);
    break;

  case ACK:
  case TRAFFIC_RESPONSE:
  case USER_NAME_RESPONSE:
  case WAVE_RESPONSE:
  case THERMAL_RESPONSE:
    /* these are server-to-client packets; ignore them */
    break;
  }
}