1 #include "../../include/freenet/trustlistrequester.h"
\r
2 #include "../../include/option.h"
\r
3 #include "../../include/stringfunctions.h"
\r
4 #include "../../include/freenet/trustlistxml.h"
\r
10 TrustListRequester::TrustListRequester()
\r
15 TrustListRequester::TrustListRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
20 void TrustListRequester::FCPConnected()
\r
22 m_requesting.clear();
\r
26 void TrustListRequester::FCPDisconnected()
\r
31 const bool TrustListRequester::HandleAllData(FCPMessage &message)
\r
34 SQLite3DB::Statement st;
\r
35 SQLite3DB::Statement trustst;
\r
36 std::vector<std::string> idparts;
\r
38 std::vector<char> data;
\r
44 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
45 StringFunctions::Convert(message["DataLength"],datalength);
\r
46 StringFunctions::Convert(idparts[1],identityid);
\r
47 StringFunctions::Convert(idparts[2],index);
\r
49 // wait for all data to be received from connection
\r
50 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
55 // if we got disconnected- return immediately
\r
56 if(m_fcp->Connected()==false)
\r
62 data.resize(datalength);
\r
63 m_fcp->ReceiveRaw(&data[0],datalength);
\r
65 // parse file into xml and update the database
\r
66 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
69 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file
\r
70 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");
\r
71 st.Bind(0,identityid);
\r
75 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
76 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust) VALUES(?,?,?,?);");
\r
77 // loop through all trust entries in xml and add to database if we don't already know them
\r
78 for(long i=0; i<xml.TrustCount(); i++)
\r
81 std::string identity;
\r
82 identity=xml.GetIdentity(i);
\r
84 st.Bind(0,identity);
\r
86 if(st.RowReturned()==false)
\r
88 m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);
\r
96 //insert trust for this identity
\r
97 trustst.Bind(0,identityid);
\r
99 trustst.Bind(2,xml.GetMessageTrust(i));
\r
100 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
105 trustst.Finalize();
\r
108 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
109 st.Bind(0,identityid);
\r
110 st.Bind(1,idparts[4]);
\r
115 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
119 // bad data - mark index
\r
120 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
121 st.Bind(0,identityid);
\r
122 st.Bind(1,idparts[4]);
\r
127 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
130 // remove this identityid from request list
\r
131 RemoveFromRequestList(identityid);
\r
137 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
140 SQLite3DB::Statement st;
\r
141 std::vector<std::string> idparts;
\r
146 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
147 StringFunctions::Convert(idparts[1],identityid);
\r
148 StringFunctions::Convert(idparts[2],index);
\r
150 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
151 if(message["Fatal"]=="true")
\r
153 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
154 st.Bind(0,identityid);
\r
155 st.Bind(1,idparts[4]);
\r
160 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
163 // remove this identityid from request list
\r
164 RemoveFromRequestList(identityid);
\r
170 const bool TrustListRequester::HandleMessage(FCPMessage &message)
\r
173 if(message["Identifier"].find("TrustListRequester")==0)
\r
175 if(message.GetName()=="DataFound")
\r
180 if(message.GetName()=="AllData")
\r
182 return HandleAllData(message);
\r
185 if(message.GetName()=="GetFailed")
\r
187 return HandleGetFailed(message);
\r
190 if(message.GetName()=="IdentifierCollision")
\r
192 // remove one of the ids from the requesting list
\r
194 std::vector<std::string> idparts;
\r
195 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
196 StringFunctions::Convert(idparts[1],identityid);
\r
197 RemoveFromRequestList(identityid);
\r
205 void TrustListRequester::Initialize()
\r
207 std::string tempval="";
\r
208 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
209 StringFunctions::Convert(tempval,m_maxrequests);
\r
210 if(m_maxrequests<1)
\r
213 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
215 if(m_maxrequests>100)
\r
217 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
219 Option::Instance()->Get("MessageBase",m_messagebase);
\r
220 m_tempdate.SetToGMTime();
\r
223 void TrustListRequester::PopulateIDList()
\r
229 date.SetToGMTime();
\r
231 // 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
232 sql="SELECT IdentityID FROM tblIdentity ";
\r
233 sql+="WHERE Name IS NOT NULL AND Name <> '' AND 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') AND ( PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust') )";
\r
234 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
236 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
241 while(st.RowReturned())
\r
243 st.ResultInt(0,id);
\r
249 void TrustListRequester::Process()
\r
251 // max is the smaller of the config value or the total number of identities we will request from
\r
252 long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
254 // try to keep up to max requests going
\r
255 if(m_requesting.size()<max)
\r
257 std::map<long,bool>::iterator i=m_ids.begin();
\r
258 while(i!=m_ids.end() && (*i).second==true)
\r
265 StartRequest((*i).first);
\r
269 // we requested from all ids in the list, repopulate the list
\r
273 // 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
274 // this will recheck for ids every minute
\r
277 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
285 void TrustListRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
287 thread->RegisterFCPConnected(this);
\r
288 thread->RegisterFCPMessageHandler(this);
\r
289 thread->RegisterPeriodicProcessor(this);
\r
292 void TrustListRequester::RemoveFromRequestList(const long identityid)
\r
294 std::vector<long>::iterator i=m_requesting.begin();
\r
295 while(i!=m_requesting.end() && (*i)!=identityid)
\r
299 if(i!=m_requesting.end())
\r
301 m_requesting.erase(i);
\r
305 void TrustListRequester::StartRequest(const long identityid)
\r
308 FCPMessage message;
\r
309 std::string publickey;
\r
311 std::string indexstr;
\r
312 std::string identityidstr;
\r
314 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
315 st.Bind(0,identityid);
\r
318 if(st.RowReturned())
\r
320 st.ResultText(0,publickey);
\r
324 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
325 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
326 st2.Bind(1,identityid);
\r
330 if(st2.RowReturned())
\r
332 if(st2.ResultNull(0)==false)
\r
334 st2.ResultInt(0,index);
\r
340 StringFunctions::Convert(index,indexstr);
\r
341 StringFunctions::Convert(identityid,identityidstr);
\r
343 message.SetName("ClientGet");
\r
344 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
345 message["Identifier"]="TrustListRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
346 message["ReturnType"]="direct";
\r
347 message["MaxSize"]="1000000"; // 1 MB
\r
349 m_fcp->SendMessage(message);
\r
351 m_requesting.push_back(identityid);
\r
355 m_ids[identityid]=true;
\r