void CRoutingZone::ReadFile(const wxString& specialNodesdat) { if (m_superZone != NULL || (m_filename.IsEmpty() && specialNodesdat.IsEmpty())) { wxFAIL; return; } bool doHaveVerifiedContacts = false; // Read in the saved contact list try { uint32_t numContacts = 0; uint32_t validContacts = 0; CFile file; if (CPath::FileExists(specialNodesdat.IsEmpty() ? m_filename : specialNodesdat) && file.Open(m_filename, CFile::read)) { // Get how many contacts in the saved list. // NOTE: Older clients put the number of contacts here... // Newer clients always have 0 here to prevent older clients from reading it. numContacts = file.ReadUInt32(); uint32_t fileVersion = 0; if (numContacts == 0) { if (file.GetLength() >= 8) { fileVersion = file.ReadUInt32(); if (fileVersion == 3) { uint32_t bootstrapEdition = file.ReadUInt32(); if (bootstrapEdition == 1) { // this is a special bootstrap-only nodes.dat, handle it in a separate reading function ReadBootstrapNodesDat(file); file.Close(); return; } } if (fileVersion >= 1 && fileVersion <= 3) { numContacts = file.ReadUInt32(); } } } else { // Don't read version 0 nodes.dat files, because they can't tell the kad version of the contacts stored. AddLogLineC(_("Failed to read nodes.dat file - too old. This version (0) is not supported anymore.")); numContacts = 0; } DEBUG_ONLY( unsigned kad1Count = 0; ) if (numContacts != 0 && numContacts * 25 <= (file.GetLength() - file.GetPosition())) { for (uint32_t i = 0; i < numContacts; i++) { CUInt128 id = file.ReadUInt128(); uint32_t ip = file.ReadUInt32(); uint16_t udpPort = file.ReadUInt16(); uint16_t tcpPort = file.ReadUInt16(); uint8_t contactVersion = 0; contactVersion = file.ReadUInt8(); CKadUDPKey kadUDPKey; bool verified = false; if (fileVersion >= 2) { kadUDPKey.ReadFromFile(file); verified = file.ReadUInt8() != 0; if (verified) { doHaveVerifiedContacts = true; } } // IP appears valid if (contactVersion > 1) { if(IsGoodIPPort(wxUINT32_SWAP_ALWAYS(ip),udpPort)) { if (!theApp->ipfilter->IsFiltered(wxUINT32_SWAP_ALWAYS(ip)) && !(udpPort == 53 && contactVersion <= 5 /*No DNS Port without encryption*/)) { // This was not a dead contact, inc counter if add was successful if (AddUnfiltered(id, ip, udpPort, tcpPort, contactVersion, kadUDPKey, verified, false, false)) { validContacts++; } } } } else { DEBUG_ONLY( kad1Count++; ) } } } file.Close(); AddLogLineN(CFormat(wxPLURAL("Read %u Kad contact", "Read %u Kad contacts", validContacts)) % validContacts); #ifdef __DEBUG__ if (kad1Count > 0) { AddDebugLogLineN(logKadRouting, CFormat(wxT("Ignored %u kad1 %s in nodes.dat file.")) % kad1Count % (kad1Count > 1 ? wxT("contacts"): wxT("contact"))); } #endif if (!doHaveVerifiedContacts) { AddDebugLogLineN(logKadRouting, wxT("No verified contacts found in nodes.dat - might be an old file version. Setting all contacts verified for this time to speed up Kad bootstrapping.")); SetAllContactsVerified(); } }
void CRoutingZone::ReadFile(CString strSpecialNodesdate) { if (m_pSuperZone != NULL || (m_sFilename.IsEmpty() && strSpecialNodesdate.IsEmpty())){ ASSERT( false ); return; } bool bDoHaveVerifiedContacts = false; // Read in the saved contact list. try { CSafeBufferedFile file; CFileException fexp; if (file.Open(strSpecialNodesdate.IsEmpty() ? m_sFilename : strSpecialNodesdate, CFile::modeRead | CFile::osSequentialScan|CFile::typeBinary|CFile::shareDenyWrite, &fexp)) { setvbuf(file.m_pStream, NULL, _IOFBF, 32768); // Get how many contacts in the saved list. // NOTE: Older clients put the number of contacts here.. // Newer clients always have 0 here to prevent older clients from reading it. uint32_t uNumContacts = file.ReadUInt32(); uint32_t uVersion = 0; if (uNumContacts == 0) { if (file.GetLength() >= 8){ uVersion = file.ReadUInt32(); if (uVersion == 3){ uint32_t nBoostrapEdition = file.ReadUInt32(); if (nBoostrapEdition == 1){ // this is a special bootstrap-only nodes.dat, handle it in a seperate reading function ReadBootstrapNodesDat(file); file.Close(); return; } } if(uVersion >= 1 && uVersion <= 3) // those version we know, others we ignore uNumContacts = file.ReadUInt32(); } else AddDebugLogLine( false, GetResString(IDS_ERR_KADCONTACTS)); } if (uNumContacts != 0 && uNumContacts * 25 <= (file.GetLength() - file.GetPosition())) { // Hide contact list in the GUI theApp.emuledlg->kademliawnd->HideContacts(); uint32_t uValidContacts = 0; CUInt128 uID; while ( uNumContacts ) { file.ReadUInt128(&uID); uint32_t uIP = file.ReadUInt32(); uint16_t uUDPPort = file.ReadUInt16(); uint16_t uTCPPort = file.ReadUInt16(); byte byType = 0; uint8_t uContactVersion = 0; if(uVersion >= 1) uContactVersion = file.ReadUInt8(); else byType = file.ReadUInt8(); CKadUDPKey kadUDPKey; bool bVerified = false; if(uVersion >= 2){ kadUDPKey.ReadFromFile(file); bVerified = file.ReadUInt8() != 0; if (bVerified) bDoHaveVerifiedContacts = true; } // IP Appears valid if( byType < 4) { uint32_t uhostIP = ntohl(uIP); if (::IsGoodIPPort(uhostIP, uUDPPort)) { if (::theApp.ipfilter->IsFiltered(uhostIP)) { if (::thePrefs.GetLogFilteredIPs()) AddDebugLogLine(false, _T("Ignored kad contact (IP=%s:%u)--read known.dat -- - IP filter (%s)") , ipstr(uhostIP), uUDPPort, ::theApp.ipfilter->GetLastHit()); } else if (uUDPPort == 53 && uContactVersion <= KADEMLIA_VERSION5_48a) /*No DNS Port without encryption*/ { if (::thePrefs.GetLogFilteredIPs()) AddDebugLogLine(false, _T("Ignored kad contact (IP=%s:%u)--read known.dat") , ipstr(uhostIP), uUDPPort); } else { // This was not a dead contact, Inc counter if add was successful if (AddUnfiltered(uID, uIP, uUDPPort, uTCPPort, uContactVersion, kadUDPKey, bVerified, false, true, false)) uValidContacts++; } } } uNumContacts--; } AddLogLine( false, GetResString(IDS_KADCONTACTSREAD), uValidContacts); if (!bDoHaveVerifiedContacts){ DebugLogWarning(_T("No verified contacts found in nodes.dat - might be an old file version. Setting all contacts verified for this time to speed up Kad bootstrapping")); SetAllContactsVerified(); } } file.Close(); } else DebugLogWarning(_T("Unable to read Kad file: %s"), m_sFilename); } catch (CFileException* e) { e->Delete(); DebugLogError(_T("CFileException in CRoutingZone::readFile")); } // Show contact list in GUI theApp.emuledlg->kademliawnd->ShowContacts(); }