void CRoutingZone::WriteFile() { try { // Write a saved contact list. CUInt128 uID; //CSafeBufferedFile file; CBufferedFileIO file; CFileException fexp; if (file.Open(m_sFilename, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary|CFile::shareDenyWrite, &fexp)) { setvbuf(file.m_pStream, NULL, _IOFBF, 32768); // The bootstrap method gets a very nice sample of contacts to save. ContactList listContacts; GetBootstrapContacts(&listContacts, 200); // Start file with 0 to prevent older clients from reading it. file.WriteUInt32(0); // Now tag it with a version which happens to be 1. file.WriteUInt32(1); file.WriteUInt32((uint32)listContacts.size()); for (ContactList::const_iterator itContactList = listContacts.begin(); itContactList != listContacts.end(); ++itContactList) { CContact* pContact = *itContactList; pContact->GetClientID(&uID); file.WriteUInt128(&uID); file.WriteUInt32(pContact->GetIPAddress()); file.WriteUInt16(pContact->GetUDPPort()); file.WriteUInt16(pContact->GetTCPPort()); file.WriteUInt8(pContact->GetVersion()); } file.Close(); //AddDebugLogLine( false, _T("Wrote %ld contact%s to file."), listContacts.size(), ((listContacts.size() == 1) ? _T("") : _T("s"))); TRACE(_T("Wrote %ld contact%s to file.\n"), listContacts.size(), ((listContacts.size() == 1) ? _T("") : _T("s"))); } } catch (CFileException* e) { e->Delete(); AddDebugLogLine(false, _T("CFileException in CRoutingZone::writeFile")); } }
void CIndexed::ReadFile(void) { try { uint32 uTotalLoad = 0; uint32 uTotalSource = 0; uint32 uTotalKeyword = 0; CUInt128 uKeyID, uID, uSourceID; CBufferedFileIO fileLoad; if(fileLoad.Open(m_sLoadFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite)) { setvbuf(fileLoad.m_pStream, NULL, _IOFBF, 32768); uint32 uVersion = fileLoad.ReadUInt32(); if(uVersion<2) { /*time_t tSaveTime = */fileLoad.ReadUInt32(); uint32 uNumLoad = fileLoad.ReadUInt32(); while(uNumLoad) { fileLoad.ReadUInt128(&uKeyID); if(AddLoad(uKeyID, fileLoad.ReadUInt32())) uTotalLoad++; uNumLoad--; } } fileLoad.Close(); } CBufferedFileIO fileKey; if (fileKey.Open(m_sKeyFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite)) { setvbuf(fileKey.m_pStream, NULL, _IOFBF, 32768); uint32 uVersion = fileKey.ReadUInt32(); if( uVersion < 3 ) { time_t tSaveTime = fileKey.ReadUInt32(); if( tSaveTime > time(NULL) ) { fileKey.ReadUInt128(&uID); if( Kademlia::CKademlia::GetPrefs()->GetKadID() == uID ) { uint32 uNumKeys = fileKey.ReadUInt32(); while( uNumKeys ) { fileKey.ReadUInt128(&uKeyID); uint32 uNumSource = fileKey.ReadUInt32(); while( uNumSource ) { fileKey.ReadUInt128(&uSourceID); uint32 uNumName = fileKey.ReadUInt32(); while( uNumName ) { CEntry* pToAdd = new Kademlia::CEntry(); pToAdd->m_bSource = false; pToAdd->m_tLifetime = fileKey.ReadUInt32(); uint32 uTotalTags = fileKey.ReadByte(); while( uTotalTags ) { CKadTag* pTag = fileKey.ReadTag(); if(pTag) { if (!pTag->m_name.Compare(TAG_FILENAME)) { pToAdd->m_fileName = pTag->GetStr(); KadTagStrMakeLower(pToAdd->m_fileName); // make lowercase, the search code expects lower case strings! // NOTE: always add the 'name' tag, even if it's stored separately in 'fileName'. the tag is still needed for answering search request pToAdd->m_listTag.push_back(pTag); } else if (!pTag->m_name.Compare(TAG_FILESIZE)) { pToAdd->m_uSize = pTag->GetInt(); // NOTE: always add the 'size' tag, even if it's stored separately in 'size'. the tag is still needed for answering search request pToAdd->m_listTag.push_back(pTag); } else if (!pTag->m_name.Compare(TAG_SOURCEIP)) { pToAdd->m_uIP = (uint32)pTag->GetInt(); pToAdd->m_listTag.push_back(pTag); } else if (!pTag->m_name.Compare(TAG_SOURCEPORT)) { pToAdd->m_uTCPPort = (uint16)pTag->GetInt(); pToAdd->m_listTag.push_back(pTag); } else if (!pTag->m_name.Compare(TAG_SOURCEUPORT)) { pToAdd->m_uUDPPort = (uint16)pTag->GetInt(); pToAdd->m_listTag.push_back(pTag); } else { pToAdd->m_listTag.push_back(pTag); } } uTotalTags--; } pToAdd->m_uKeyID.SetValue(uKeyID); pToAdd->m_uSourceID.SetValue(uSourceID); uint8 uLoad; if(AddKeyword(uKeyID, uSourceID, pToAdd, uLoad)) uTotalKeyword++; else delete pToAdd; uNumName--; } uNumSource--; } uNumKeys--; } } } } fileKey.Close(); } CBufferedFileIO fileSource; if (fileSource.Open(m_sSourceFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite)) { setvbuf(fileSource.m_pStream, NULL, _IOFBF, 32768); uint32 uVersion = fileSource.ReadUInt32(); if( uVersion < 3 ) { time_t tSaveTime = fileSource.ReadUInt32(); if( tSaveTime > time(NULL) ) { uint32 uNumKeys = fileSource.ReadUInt32(); while( uNumKeys ) { fileSource.ReadUInt128(&uKeyID); uint32 uNumSource = fileSource.ReadUInt32(); while( uNumSource ) { fileSource.ReadUInt128(&uSourceID); uint32 uNumName = fileSource.ReadUInt32(); while( uNumName ) { CEntry* pToAdd = new Kademlia::CEntry(); pToAdd->m_bSource = true; pToAdd->m_tLifetime = fileSource.ReadUInt32(); uint32 uTotalTags = fileSource.ReadByte(); while( uTotalTags ) { CKadTag* pTag = fileSource.ReadTag(); if(pTag) { if (!pTag->m_name.Compare(TAG_SOURCEIP)) { pToAdd->m_uIP = (uint32)pTag->GetInt(); pToAdd->m_listTag.push_back(pTag); } else if (!pTag->m_name.Compare(TAG_SOURCEPORT)) { pToAdd->m_uTCPPort = (uint16)pTag->GetInt(); pToAdd->m_listTag.push_back(pTag); } else if (!pTag->m_name.Compare(TAG_SOURCEUPORT)) { pToAdd->m_uUDPPort = (uint16)pTag->GetInt(); pToAdd->m_listTag.push_back(pTag); } else { pToAdd->m_listTag.push_back(pTag); } } uTotalTags--; } pToAdd->m_uKeyID.SetValue(uKeyID); pToAdd->m_uSourceID.SetValue(uSourceID); uint8 uLoad; if(AddSources(uKeyID, uSourceID, pToAdd, uLoad)) uTotalSource++; else delete pToAdd; uNumName--; } uNumSource--; } uNumKeys--; } } } fileSource.Close(); m_uTotalIndexSource = uTotalSource; m_uTotalIndexKeyword = uTotalKeyword; m_uTotalIndexLoad = uTotalLoad; AddDebugLogLine( false, _T("Read %u source, %u keyword, and %u load entries"), uTotalSource, uTotalKeyword, uTotalLoad); } } catch ( CIOException *ioe ) { AddDebugLogLine( false, _T("Exception in CIndexed::readFile (IO error(%i))"), ioe->m_iCause); ioe->Delete(); } catch (...) { AddDebugLogLine(false, _T("Exception in CIndexed::readFile")); } }
CIndexed::~CIndexed() { try { uint32 uTotalSource = 0; uint32 uTotalKey = 0; uint32 uTotalLoad = 0; CBufferedFileIO fileLoad; if(fileLoad.Open(m_sLoadFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyWrite)) { setvbuf(fileLoad.m_pStream, NULL, _IOFBF, 32768); uint32 uVersion = 1; fileLoad.WriteUInt32(uVersion); fileLoad.WriteUInt32(time(NULL)); fileLoad.WriteUInt32(m_mapLoad.GetCount()); POSITION pos1 = m_mapLoad.GetStartPosition(); while( pos1 != NULL ) { Load* pLoad; CCKey key1; m_mapLoad.GetNextAssoc( pos1, key1, pLoad ); fileLoad.WriteUInt128(pLoad->uKeyID); fileLoad.WriteUInt32(pLoad->uTime); uTotalLoad++; delete pLoad; } fileLoad.Close(); } CBufferedFileIO fileSource; if (fileSource.Open(m_sSourceFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyWrite)) { setvbuf(fileSource.m_pStream, NULL, _IOFBF, 32768); uint32 uVersion = 2; fileSource.WriteUInt32(uVersion); fileSource.WriteUInt32(time(NULL)+KADEMLIAREPUBLISHTIMES); fileSource.WriteUInt32(m_mapSources.GetCount()); POSITION pos1 = m_mapSources.GetStartPosition(); while( pos1 != NULL ) { CCKey key1; SrcHash* pCurrSrcHash; m_mapSources.GetNextAssoc( pos1, key1, pCurrSrcHash ); fileSource.WriteUInt128(pCurrSrcHash->uKeyID); CKadSourcePtrList* keyHashSrcMap = &pCurrSrcHash->ptrlistSource; fileSource.WriteUInt32(keyHashSrcMap->GetCount()); POSITION pos2 = keyHashSrcMap->GetHeadPosition(); while( pos2 != NULL ) { Source* pCurrSource = keyHashSrcMap->GetNext(pos2); fileSource.WriteUInt128(pCurrSource->uSourceID); CKadEntryPtrList* srcEntryList = &pCurrSource->ptrlEntryList; fileSource.WriteUInt32(srcEntryList->GetCount()); for(POSITION pos3 = srcEntryList->GetHeadPosition(); pos3 != NULL; ) { CEntry* pCurrName = srcEntryList->GetNext(pos3); fileSource.WriteUInt32(pCurrName->m_tLifetime); fileSource.WriteTagList(pCurrName->m_listTag); delete pCurrName; uTotalSource++; } delete pCurrSource; } delete pCurrSrcHash; } fileSource.Close(); } CBufferedFileIO fileKey; if (fileKey.Open(m_sKeyFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyWrite)) { setvbuf(fileKey.m_pStream, NULL, _IOFBF, 32768); uint32 uVersion = 2; fileKey.WriteUInt32(uVersion); fileKey.WriteUInt32(time(NULL)+KADEMLIAREPUBLISHTIMEK); fileKey.WriteUInt128(Kademlia::CKademlia::GetPrefs()->GetKadID()); fileKey.WriteUInt32(m_mapKeyword.GetCount()); POSITION pos1 = m_mapKeyword.GetStartPosition(); while( pos1 != NULL ) { CCKey key1; KeyHash* pCurrKeyHash; m_mapKeyword.GetNextAssoc( pos1, key1, pCurrKeyHash ); fileKey.WriteUInt128(pCurrKeyHash->uKeyID); CSourceKeyMap* keySrcKeyMap = &pCurrKeyHash->mapSource; fileKey.WriteUInt32(keySrcKeyMap->GetCount()); POSITION pos2 = keySrcKeyMap->GetStartPosition(); while( pos2 != NULL ) { Source* pCurrSource; CCKey key2; keySrcKeyMap->GetNextAssoc( pos2, key2, pCurrSource ); fileKey.WriteUInt128(pCurrSource->uSourceID); CKadEntryPtrList* srcEntryList = &pCurrSource->ptrlEntryList; fileKey.WriteUInt32(srcEntryList->GetCount()); for(POSITION pos3 = srcEntryList->GetHeadPosition(); pos3 != NULL; ) { CEntry* pCurrName = srcEntryList->GetNext(pos3); fileKey.WriteUInt32(pCurrName->m_tLifetime); fileKey.WriteTagList(pCurrName->m_listTag); delete pCurrName; uTotalKey++; } delete pCurrSource; } delete pCurrKeyHash; } fileKey.Close(); } AddDebugLogLine( false, _T("Wrote %u source, %u keyword, and %u load entries"), uTotalSource, uTotalKey, uTotalLoad); POSITION pos1 = m_mapNotes.GetStartPosition(); while( pos1 != NULL ) { CCKey key1; SrcHash* pCurrNoteHash; m_mapNotes.GetNextAssoc( pos1, key1, pCurrNoteHash ); CKadSourcePtrList* keyHashNoteMap = &pCurrNoteHash->ptrlistSource; POSITION pos2 = keyHashNoteMap->GetHeadPosition(); while( pos2 != NULL ) { Source* pCurrNote = keyHashNoteMap->GetNext(pos2); CKadEntryPtrList* noteEntryList = &pCurrNote->ptrlEntryList; for(POSITION pos3 = noteEntryList->GetHeadPosition(); pos3 != NULL; ) { delete noteEntryList->GetNext(pos3); } delete pCurrNote; } delete pCurrNoteHash; } } catch ( CIOException *ioe ) { AddDebugLogLine( false, _T("Exception in CIndexed::~CIndexed (IO error(%i))"), ioe->m_iCause); ioe->Delete(); } catch (...) { AddDebugLogLine(false, _T("Exception in CIndexed::~CIndexed")); } }
void CRoutingZone::ReadFile() { // Read in the saved contact list. try { // Hide contact list in the GUI // theApp.emuledlg->kademliawnd->HideContacts(); CKademlia::GetListCtrl()->ShowWindow(SW_HIDE); CKademlia::GetHistogramCtrl()->ShowWindow(SW_HIDE); //CSafeBufferedFile file; CBufferedFileIO file; CFileException fexp; if (file.Open(m_sFilename, 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 uNumContacts = file.ReadUInt32(); uint32 uVersion = 0; if (uNumContacts == 0) { try { uVersion = file.ReadUInt32(); if(uVersion == 1) uNumContacts = file.ReadUInt32(); } catch(...) { //AddDebugLogLine( false, GetResString(IDS_ERR_KADCONTACTS)); TRACE(_T("No contacts found, please bootstrap, or download a nodes.dat file.\n")); } } if (uNumContacts != 0) { uint32 uValidContacts = 0; CUInt128 uID; while ( uNumContacts ) { file.ReadUInt128(&uID); uint32 uIP = file.ReadUInt32(); uint16 uUDPPort = file.ReadUInt16(); uint16 uTCPPort = file.ReadUInt16(); uint8 uContactVersion = 0; byte byType = 0; if(uVersion == 1) uContactVersion = file.ReadUInt8(); else byType = file.ReadUInt8(); // IP Appears valid if( byType < 4) { uint32 uhostIP = ntohl(uIP); /*----------> xt if (::IsGoodIPPort(uhostIP, uUDPPort)) { if (::theApp.ipfilter->IsFiltered(uhostIP)) { if (::thePrefs.GetLogFilteredIPs()) AddDebugLogLine(false, _T("Ignored kad contact (IP=%s)--read known.dat -- - IP filter (%s)") , ipstr(uhostIP), ::theApp.ipfilter->GetLastHit()); } else { // This was not a dead contact, Inc counter if add was successful if (AddUnfiltered(uID, uIP, uUDPPort, uTCPPort, uContactVersion, false)) uValidContacts++; } } ----------< xt */ if (AddUnfiltered(uID, uIP, uUDPPort, uTCPPort, uContactVersion, false)) uValidContacts++; } uNumContacts--; } //AddLogLine( false, GetResString(IDS_KADCONTACTSREAD), uValidContacts); TRACE(_T("Read %ld contacts from file.\n"), uValidContacts); } file.Close(); } } catch (CFileException* e) { e->Delete(); AddDebugLogLine(false, _T("CFileException in CRoutingZone::readFile")); } // Show contact list in GUI // theApp.emuledlg->kademliawnd->ShowContacts(); CKademlia::GetListCtrl()->ShowWindow(SW_SHOW); CKademlia::GetHistogramCtrl()->ShowWindow(SW_SHOW); }