📄 kademliaudplistener.cpp
字号:
} else { //More than one port tag found delete tag; } } else { //TODO: Filter tags entry->AddTag(tag); } } tags--; } if (!strInfo.IsEmpty()) { AddDebugLogLineM(false, logClientKadUDP, strInfo); } } catch(...) { //printf("Error on count %i tag %i\n",totalcount-count, totaltags-tags); //DebugClientOutput(wxT("CKademliaUDPListener::processPublishRequest"),ip,port,packetData,lenPacket); delete entry; throw; } if (entry->m_bSource == true) { entry->m_tLifeTime = (uint32_t)time(NULL) + KADEMLIAREPUBLISHTIMES; CEntry *sourceEntry = entry->Copy(); // "downcast" since we didnt knew before if this was a keyword, Kad1 support gets removed soon anyway no need for a beautiful solution here delete entry; entry = NULL; if (indexed->AddSources(file, target, sourceEntry, load)) { flag = true; } else { delete sourceEntry; sourceEntry = NULL; } } else { entry->m_tLifeTime = (uint32_t)time(NULL) + KADEMLIAREPUBLISHTIMEK; if (indexed->AddKeyword(file, target, entry, load)) { // This makes sure we send a publish response.. // This also makes sure we index all the files for this keyword. flag = true; } else { //We already indexed the maximum number of keywords. //We do not index anymore but we still send a success.. //Reason: Because if a VERY busy node tells the publisher it failed, //this busy node will spread to all the surrounding nodes causing popular //keywords to be stored on MANY nodes.. //So, once we are full, we will periodically clean our list until we can //begin storing again.. flag = true; delete entry; entry = NULL; } } count--; } if (flag) { CMemFile packetdata(17); packetdata.WriteUInt128(file); packetdata.WriteUInt8(load); DebugSend(KadPublishRes, ip, port); SendPacket(packetdata, KADEMLIA_PUBLISH_RES, ip, port, 0, NULL); }}// KADEMLIA2_PUBLISH_KEY_REQ// Used in Kad2.0 onlyvoid CKademliaUDPListener::Process2PublishKeyRequest(const uint8_t *packetData, uint32_t lenPacket, uint32_t ip, uint16_t port, const CKadUDPKey& senderKey){ //Used Pointers CIndexed *indexed = CKademlia::GetIndexed(); // check if we are UDP firewalled if (CUDPFirewallTester::IsFirewalledUDP(true)) { //We are firewalled. We should not index this entry and give publisher a false report. return; } CMemFile bio(packetData, lenPacket); CUInt128 file = bio.ReadUInt128(); CUInt128 distance(CKademlia::GetPrefs()->GetKadID()); distance.XOR(file); // Shouldn't LAN IPs already be filtered? if (thePrefs::FilterLanIPs() && distance.Get32BitChunk(0) > SEARCHTOLERANCE) { return; } wxString strInfo; uint16_t count = bio.ReadUInt16(); uint8_t load = 0; while (count > 0) { strInfo.Clear(); CUInt128 target = bio.ReadUInt128(); Kademlia::CKeyEntry* entry = new Kademlia::CKeyEntry(); try { entry->m_uIP = ip; entry->m_uUDPport = port; entry->m_uKeyID.SetValue(file); entry->m_uSourceID.SetValue(target); entry->m_tLifeTime = (uint32_t)time(NULL) + KADEMLIAREPUBLISHTIMEK; entry->m_bSource = false; uint32_t tags = bio.ReadUInt8(); while (tags > 0) { CTag* tag = bio.ReadTag(); if (tag) { if (!tag->GetName().Cmp(TAG_FILENAME)) { if (entry->GetCommonFileName().IsEmpty()) { entry->SetFileName(tag->GetStr()); strInfo += CFormat(wxT(" Name=\"%s\"")) % entry->GetCommonFileName(); } delete tag; // tag is no longer stored, but membervar is used } else if (!tag->GetName().Cmp(TAG_FILESIZE)) { if (entry->m_uSize == 0) { if (tag->IsBsob() && tag->GetBsobSize() == 8) { entry->m_uSize = PeekUInt64(tag->GetBsob()); } else { entry->m_uSize = tag->GetInt(); } strInfo += wxString::Format(wxT(" Size=%") wxLongLongFmtSpec wxT("u"), entry->m_uSize); } delete tag; // tag is no longer stored, but membervar is used } else { //TODO: Filter tags entry->AddTag(tag); } } tags--; } if (!strInfo.IsEmpty()) { AddDebugLogLineM(false, logClientKadUDP, strInfo); } } catch(...) { //DebugClientOutput(wxT("CKademliaUDPListener::Process2PublishKeyRequest"),ip,port,packetData,lenPacket); delete entry; throw; } if (!indexed->AddKeyword(file, target, entry, load)) { //We already indexed the maximum number of keywords. //We do not index anymore but we still send a success.. //Reason: Because if a VERY busy node tells the publisher it failed, //this busy node will spread to all the surrounding nodes causing popular //keywords to be stored on MANY nodes.. //So, once we are full, we will periodically clean our list until we can //begin storing again.. delete entry; entry = NULL; } count--; } CMemFile packetdata(17); packetdata.WriteUInt128(file); packetdata.WriteUInt8(load); DebugSend(Kad2PublishRes, ip, port); SendPacket(packetdata, KADEMLIA2_PUBLISH_RES, ip, port, senderKey, NULL);}// KADEMLIA2_PUBLISH_SOURCE_REQ// Used in Kad2.0 onlyvoid CKademliaUDPListener::Process2PublishSourceRequest(const uint8_t *packetData, uint32_t lenPacket, uint32_t ip, uint16_t port, const CKadUDPKey& senderKey){ //Used Pointers CIndexed *indexed = CKademlia::GetIndexed(); // check if we are UDP firewalled if (CUDPFirewallTester::IsFirewalledUDP(true)) { //We are firewalled. We should not index this entry and give publisher a false report. return; } CMemFile bio(packetData, lenPacket); CUInt128 file = bio.ReadUInt128(); CUInt128 distance(CKademlia::GetPrefs()->GetKadID()); distance.XOR(file); if (thePrefs::FilterLanIPs() && distance.Get32BitChunk(0) > SEARCHTOLERANCE) { return; } wxString strInfo; uint8_t load = 0; bool flag = false; CUInt128 target = bio.ReadUInt128(); Kademlia::CEntry* entry = new Kademlia::CEntry(); try { entry->m_uIP = ip; entry->m_uUDPport = port; entry->m_uKeyID.SetValue(file); entry->m_uSourceID.SetValue(target); entry->m_bSource = false; entry->m_tLifeTime = (uint32_t)time(NULL) + KADEMLIAREPUBLISHTIMES; bool addUDPPortTag = true; uint32_t tags = bio.ReadUInt8(); while (tags > 0) { CTag* tag = bio.ReadTag(); if (tag) { if (!tag->GetName().Cmp(TAG_SOURCETYPE)) { if (entry->m_bSource == false) { entry->AddTag(new CTagVarInt(TAG_SOURCEIP, entry->m_uIP)); entry->AddTag(tag); entry->m_bSource = true; } else { //More than one sourcetype tag found. delete tag; } } else if (!tag->GetName().Cmp(TAG_FILESIZE)) { if (entry->m_uSize == 0) { if (tag->IsBsob() && tag->GetBsobSize() == 8) { entry->m_uSize = PeekUInt64(tag->GetBsob()); } else { entry->m_uSize = tag->GetInt(); } strInfo += wxString::Format(wxT(" Size=%") wxLongLongFmtSpec wxT("u"), entry->m_uSize); } delete tag; } else if (!tag->GetName().Cmp(TAG_SOURCEPORT)) { if (entry->m_uTCPport == 0) { entry->m_uTCPport = (uint16_t)tag->GetInt(); entry->AddTag(tag); } else { //More than one port tag found delete tag; } } else if (!tag->GetName().Cmp(TAG_SOURCEUPORT)) { if (addUDPPortTag && tag->IsInt() && tag->GetInt() != 0) { entry->m_uUDPport = (uint16_t)tag->GetInt(); entry->AddTag(tag); addUDPPortTag = false; } else { //More than one udp port tag found delete tag; } } else { //TODO: Filter tags entry->AddTag(tag); } } tags--; } if (addUDPPortTag) { entry->AddTag(new CTagVarInt(TAG_SOURCEUPORT, entry->m_uUDPport)); } if (!strInfo.IsEmpty()) { AddDebugLogLineM(false, logClientKadUDP, strInfo); } } catch(...) { //DebugClientOutput(wxT("CKademliaUDPListener::Process2PublishSourceRequest"),ip,port,packetData,lenPacket); delete entry; throw; } if (entry->m_bSource == true) { if (indexed->AddSources(file, target, entry, load)) { flag = true; } else { delete entry; entry = NULL; } } else { delete entry; entry = NULL; } if (flag) { CMemFile packetdata(17); packetdata.WriteUInt128(file); packetdata.WriteUInt8(load); DebugSend(Kad2PublishRes, ip, port); SendPacket(packetdata, KADEMLIA2_PUBLISH_RES, ip, port, senderKey, NULL); }}// KADEMLIA_PUBLISH_RES// Used only by Kad1.0void CKademliaUDPListener::ProcessPublishResponse(const uint8_t *packetData, uint32_t lenPacket, uint32_t ip){ // Verify packet is expected size CHECK_PACKET_MIN_SIZE(16); CHECK_TRACKED_PACKET(KADEMLIA_PUBLISH_REQ); CMemFile bio(packetData, lenPacket); CUInt128 file = bio.ReadUInt128(); bool loadResponse = false; uint8_t load = 0; if (bio.GetLength() > bio.GetPosition()) { loadResponse = true; load = bio.ReadUInt8(); } CSearchManager::ProcessPublishResult(file, load, loadResponse);}// KADEMLIA2_PUBLISH_RES// Used only by Kad2.0void CKademliaUDPListener::Process2PublishResponse(const uint8_t *packetData, uint32_t lenPacket, uint32_t ip, uint16_t port, const CKadUDPKey& senderKey){ if (!IsOnOutTrackList(ip, KADEMLIA2_PUBLISH_KEY_REQ) && !IsOnOutTrackList(ip, KADEMLIA2_PUBLISH_SOURCE_REQ) && !IsOnOutTrackList(ip, KADEMLIA2_PUBLISH_NOTES_REQ)) { throw wxString::Format(wxT("***NOTE: Received unrequested response packet, size (%u) in "), lenPacket) + wxString::FromAscii(__FUNCTION__); } CMemFile bio(packetData, lenPacket); CUInt128 file = bio.ReadUInt128(); uint8_t load = bio.ReadUInt8(); CSearchManager::ProcessPublishResult(file, load, true); if (bio.GetLength() > bio.GetPosition()) { // for future use uint8_t options = bio.ReadUInt8(); bool requestACK = (options & 0x01) > 0; if (requestACK && !senderKey.IsEmpty()) { DebugSend(Kad2PublishResAck, ip, port); SendNullPacket(KADEMLIA2_PUBLISH_RES_ACK, ip, port, senderKey, NULL); } }}// KADEMLIA_SEARCH_NOTES_REQ// Used only by Kad1.0void CKademliaUDPListener::ProcessSearchNotesRequest(const uint8_t *packetData, uint32_t lenPacket, uint32_t ip, uint16_t port){ // Verify packet is expected size CHECK_PACKET_MIN_SIZE(32); CMemFile bio(packetData, lenPacket); CUInt128 target = bio.ReadUInt128(); // This info is currently not used. // CUInt128 source = bio.ReadUInt128(); CKademlia::GetIndexed()->SendValidNoteResult(target, ip, port, false, 0, 0);}// KADEMLIA2_SEARCH_NOTES_REQ// Used only by Kad2.0void CKademliaUDPListener::Process2SearchNotesRequest(const uint8_t *packetData, uint32_t lenPacket, uint32_t ip, uint16_t port, const CKadUDPKey& senderKey){ CMemFile bio(packetData, lenPacket); CUInt128 target = bio.ReadUInt128(); uint64_t fileSize = bio.ReadUInt64(); CKademlia::GetIndexed()->SendValidNoteResult(target, ip, port, true, fileSize, senderKey);}// KADEMLIA_SEARCH_NOTES_RES// Used only by Kad1.0void CKademliaUDPListener::ProcessSearchNotesResponse(const uint8_t *packetData, uint32_t lenPacket, uint32_t ip){ // Verify packet is expected size CHECK_PACKET_MIN_SIZE(37); CHECK_TRACKED_PACKET(KADEMLIA_SEARCH_NOTES_REQ); // What search does this relate to CMemFile bio(packetData, lenPacket); CUInt128 target = bio.ReadUInt128(); uint16_t count = bio.ReadUInt16(); while (count > 0) { // What is the answer CUInt128 answer = bio.ReadUInt128(); // Get info about answer // NOTE: this is the one and only place in Kad where we allow string conversion to local code page in // case we did not receive an UTF8 string. this is for backward compatibility for search results which are /
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -