+#include "../../include/freenet/trustlistrequester.h"\r
+#include "../../include/option.h"\r
+#include "../../include/stringfunctions.h"\r
+#include "../../include/freenet/trustlistxml.h"\r
+\r
+#ifdef XMEM\r
+ #include <xmem.h>\r
+#endif\r
+\r
+TrustListRequester::TrustListRequester()\r
+{\r
+ Initialize();\r
+}\r
+\r
+TrustListRequester::TrustListRequester(FCPv2 *fcp):IFCPConnected(fcp)\r
+{\r
+ Initialize();\r
+}\r
+\r
+void TrustListRequester::FCPConnected()\r
+{\r
+ m_requesting.clear();\r
+ PopulateIDList();\r
+}\r
+\r
+void TrustListRequester::FCPDisconnected()\r
+{\r
+ \r
+}\r
+\r
+const bool TrustListRequester::HandleAllData(FCPMessage &message)\r
+{\r
+ DateTime now;\r
+ SQLite3DB::Statement st;\r
+ std::vector<std::string> idparts;\r
+ long datalength;\r
+ std::vector<char> data;\r
+ TrustListXML xml;\r
+ long identityid;\r
+ long index;\r
+\r
+ now.SetToGMTime();\r
+ StringFunctions::Split(message["Identifier"],"|",idparts);\r
+ StringFunctions::Convert(message["DataLength"],datalength);\r
+ StringFunctions::Convert(idparts[1],identityid);\r
+ StringFunctions::Convert(idparts[2],index);\r
+\r
+ // wait for all data to be received from connection\r
+ while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
+ {\r
+ m_fcp->Update(1);\r
+ }\r
+\r
+ // if we got disconnected- return immediately\r
+ if(m_fcp->Connected()==false)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ // receive the file\r
+ data.resize(datalength);\r
+ m_fcp->ReceiveRaw(&data[0],datalength);\r
+\r
+ // parse file into xml and update the database\r
+ if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
+ {\r
+ st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");\r
+ // loop through all trust entries in xml and add to database if we don't already know them\r
+ for(long i=0; i<xml.TrustCount(); i++)\r
+ {\r
+ std::string identity;\r
+ identity=xml.GetIdentity(i);\r
+\r
+ //TODO get the trust levels as well\r
+\r
+ st.Bind(0,identity);\r
+ st.Step();\r
+ if(st.RowReturned()==false)\r
+ {\r
+ m_db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');");\r
+ }\r
+ st.Reset();\r
+ }\r
+\r
+ st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");\r
+ st.Bind(0,identityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+ st.Finalize();\r
+\r
+ m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" parsed Identity XML file : "+message["Identifier"]);\r
+ }\r
+ else\r
+ {\r
+ // bad data - mark index\r
+ st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
+ st.Bind(0,identityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+ st.Finalize();\r
+\r
+ m_log->WriteLog(LogFile::LOGLEVEL_ERROR,__FUNCTION__" error parsing TrustList XML file : "+message["Identifier"]);\r
+ }\r
+\r
+ // remove this identityid from request list\r
+ RemoveFromRequestList(identityid);\r
+\r
+ return true;\r
+\r
+}\r
+\r
+const bool TrustListRequester::HandleGetFailed(FCPMessage &message)\r
+{\r
+ DateTime now;\r
+ SQLite3DB::Statement st;\r
+ std::vector<std::string> idparts;\r
+ long identityid;\r
+ long index;\r
+\r
+ now.SetToGMTime();\r
+ StringFunctions::Split(message["Identifier"],"|",idparts);\r
+ StringFunctions::Convert(idparts[1],identityid);\r
+ StringFunctions::Convert(idparts[2],index); \r
+\r
+ // if this is a fatal error - insert index into database so we won't try to download this index again\r
+ if(message["Fatal"]=="true")\r
+ {\r
+ st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
+ st.Bind(0,identityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+ st.Finalize();\r
+\r
+ m_log->WriteLog(LogFile::LOGLEVEL_ERROR,__FUNCTION__" fatal error requesting "+message["Identifier"]);\r
+ }\r
+\r
+ // remove this identityid from request list\r
+ RemoveFromRequestList(identityid);\r
+\r
+ return true;\r
+\r
+}\r
+\r
+const bool TrustListRequester::HandleMessage(FCPMessage &message)\r
+{\r
+\r
+ if(message["Identifier"].find("TrustListRequester")==0)\r
+ {\r
+ if(message.GetName()=="DataFound")\r
+ {\r
+ return true;\r
+ }\r
+\r
+ if(message.GetName()=="AllData")\r
+ {\r
+ return HandleAllData(message);\r
+ }\r
+\r
+ if(message.GetName()=="GetFailed")\r
+ {\r
+ return HandleGetFailed(message);\r
+ }\r
+\r
+ if(message.GetName()=="IdentifierCollision")\r
+ {\r
+ // remove one of the ids from the requesting list\r
+ long identityid=0;\r
+ std::vector<std::string> idparts;\r
+ StringFunctions::Split(message["Identifier"],"|",idparts);\r
+ StringFunctions::Convert(idparts[1],identityid);\r
+ RemoveFromRequestList(identityid);\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+void TrustListRequester::Initialize()\r
+{\r
+ std::string tempval="";\r
+ Option::instance()->Get("MaxIdentityRequests",tempval);\r
+ StringFunctions::Convert(tempval,m_maxrequests);\r
+ if(m_maxrequests<1)\r
+ {\r
+ m_maxrequests=1;\r
+ m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");\r
+ }\r
+ if(m_maxrequests>100)\r
+ {\r
+ m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");\r
+ }\r
+ Option::instance()->Get("MessageBase",m_messagebase);\r
+ m_tempdate.SetToGMTime();\r
+}\r
+\r
+void TrustListRequester::PopulateIDList()\r
+{\r
+ DateTime date;\r
+ int id;\r
+\r
+ date.SetToGMTime();\r
+\r
+ // select identities we want to query (we've seen them today and they are publishing trust list) - sort by their trust level (descending) with secondary sort on how long ago we saw them (ascending)\r
+ SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+date.Format("%Y-%m-%d")+"' AND PublishTrustList='true' AND LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust') ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");\r
+ st.Step();\r
+\r
+ m_ids.clear();\r
+\r
+ while(st.RowReturned())\r
+ {\r
+ st.ResultInt(0,id);\r
+ m_ids[id]=false;\r
+ st.Step();\r
+ }\r
+}\r
+\r
+void TrustListRequester::Process()\r
+{\r
+ // max is the smaller of the config value or the total number of identities we will request from\r
+ long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;\r
+\r
+ // try to keep up to max requests going\r
+ if(m_requesting.size()<max)\r
+ {\r
+ std::map<long,bool>::iterator i=m_ids.begin();\r
+ while(i!=m_ids.end() && (*i).second==true)\r
+ {\r
+ i++;\r
+ }\r
+\r
+ if(i!=m_ids.end())\r
+ {\r
+ StartRequest((*i).first);\r
+ }\r
+ else\r
+ {\r
+ // we requested from all ids in the list, repopulate the list\r
+ PopulateIDList();\r
+ }\r
+ }\r
+ // special case - if there were 0 identities on the list when we started then we will never get a chance to repopulate the list\r
+ // this will recheck for ids every minute\r
+ DateTime now;\r
+ now.SetToGMTime();\r
+ if(m_tempdate<(now-(1.0/1440.0)))\r
+ {\r
+ PopulateIDList();\r
+ m_tempdate=now;\r
+ }\r
+\r
+}\r
+\r
+void TrustListRequester::RegisterWithThread(FreenetMasterThread *thread)\r
+{\r
+ thread->RegisterFCPConnected(this);\r
+ thread->RegisterFCPMessageHandler(this);\r
+ thread->RegisterPeriodicProcessor(this);\r
+}\r
+\r
+void TrustListRequester::RemoveFromRequestList(const long identityid)\r
+{\r
+ std::vector<long>::iterator i=m_requesting.begin();\r
+ while(i!=m_requesting.end() && (*i)!=identityid)\r
+ {\r
+ i++;\r
+ }\r
+ if(i!=m_requesting.end())\r
+ {\r
+ m_requesting.erase(i);\r
+ }\r
+}\r
+\r
+void TrustListRequester::StartRequest(const long identityid)\r
+{\r
+ DateTime now;\r
+ FCPMessage message;\r
+ std::string publickey;\r
+ int index;\r
+ std::string indexstr;\r
+ std::string identityidstr;\r
+\r
+ SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
+ st.Bind(0,identityid);\r
+ st.Step();\r
+\r
+ if(st.RowReturned())\r
+ {\r
+ st.ResultText(0,publickey);\r
+\r
+ now.SetToGMTime();\r
+\r
+ SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");\r
+ st2.Bind(0,now.Format("%Y-%m-%d"));\r
+ st2.Bind(1,identityid);\r
+ st2.Step();\r
+\r
+ index=0;\r
+ if(st2.RowReturned())\r
+ {\r
+ if(st2.ResultNull(0)==false)\r
+ {\r
+ st2.ResultInt(0,index);\r
+ index++;\r
+ }\r
+ }\r
+ st2.Finalize();\r
+\r
+ StringFunctions::Convert(index,indexstr);\r
+ StringFunctions::Convert(identityid,identityidstr);\r
+\r
+ message.SetName("ClientGet");\r
+ message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
+ message["Identifier"]="TrustListRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
+ message["ReturnType"]="direct";\r
+ message["MaxSize"]="1000000"; // 1 MB\r
+\r
+ m_fcp->SendMessage(message);\r
+\r
+ m_requesting.push_back(identityid);\r
+ }\r
+ st.Finalize();\r
+\r
+ m_ids[identityid]=true;\r
+\r
+}\r