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 if(xml.GetMessageTrust(i)==-1)
\r
105 trustst.Bind(2,xml.GetMessageTrust(i));
\r
107 if(xml.GetTrustListTrust(i)==-1)
\r
113 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
119 trustst.Finalize();
\r
122 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
123 st.Bind(0,identityid);
\r
124 st.Bind(1,idparts[4]);
\r
129 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
133 // bad data - mark index
\r
134 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
135 st.Bind(0,identityid);
\r
136 st.Bind(1,idparts[4]);
\r
141 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
144 // remove this identityid from request list
\r
145 RemoveFromRequestList(identityid);
\r
151 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
154 SQLite3DB::Statement st;
\r
155 std::vector<std::string> idparts;
\r
160 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
161 StringFunctions::Convert(idparts[1],identityid);
\r
162 StringFunctions::Convert(idparts[2],index);
\r
164 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
165 if(message["Fatal"]=="true")
\r
167 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
168 st.Bind(0,identityid);
\r
169 st.Bind(1,idparts[4]);
\r
174 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
177 // remove this identityid from request list
\r
178 RemoveFromRequestList(identityid);
\r
184 const bool TrustListRequester::HandleMessage(FCPMessage &message)
\r
187 if(message["Identifier"].find("TrustListRequester")==0)
\r
189 if(message.GetName()=="DataFound")
\r
194 if(message.GetName()=="AllData")
\r
196 return HandleAllData(message);
\r
199 if(message.GetName()=="GetFailed")
\r
201 return HandleGetFailed(message);
\r
204 if(message.GetName()=="IdentifierCollision")
\r
206 // remove one of the ids from the requesting list
\r
208 std::vector<std::string> idparts;
\r
209 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
210 StringFunctions::Convert(idparts[1],identityid);
\r
211 RemoveFromRequestList(identityid);
\r
219 void TrustListRequester::Initialize()
\r
221 std::string tempval="";
\r
222 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
223 StringFunctions::Convert(tempval,m_maxrequests);
\r
224 if(m_maxrequests<1)
\r
227 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
229 if(m_maxrequests>100)
\r
231 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
233 Option::Instance()->Get("MessageBase",m_messagebase);
\r
234 m_tempdate.SetToGMTime();
\r
237 void TrustListRequester::PopulateIDList()
\r
243 date.SetToGMTime();
\r
245 // 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
246 sql="SELECT IdentityID FROM tblIdentity ";
\r
247 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
248 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
250 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
255 while(st.RowReturned())
\r
257 st.ResultInt(0,id);
\r
263 void TrustListRequester::Process()
\r
265 // max is the smaller of the config value or the total number of identities we will request from
\r
266 long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
268 // try to keep up to max requests going
\r
269 if(m_requesting.size()<max)
\r
271 std::map<long,bool>::iterator i=m_ids.begin();
\r
272 while(i!=m_ids.end() && (*i).second==true)
\r
279 StartRequest((*i).first);
\r
283 // we requested from all ids in the list, repopulate the list
\r
287 // 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
288 // this will recheck for ids every minute
\r
291 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
299 void TrustListRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
301 thread->RegisterFCPConnected(this);
\r
302 thread->RegisterFCPMessageHandler(this);
\r
303 thread->RegisterPeriodicProcessor(this);
\r
306 void TrustListRequester::RemoveFromRequestList(const long identityid)
\r
308 std::vector<long>::iterator i=m_requesting.begin();
\r
309 while(i!=m_requesting.end() && (*i)!=identityid)
\r
313 if(i!=m_requesting.end())
\r
315 m_requesting.erase(i);
\r
319 void TrustListRequester::StartRequest(const long identityid)
\r
322 FCPMessage message;
\r
323 std::string publickey;
\r
325 std::string indexstr;
\r
326 std::string identityidstr;
\r
328 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
329 st.Bind(0,identityid);
\r
332 if(st.RowReturned())
\r
334 st.ResultText(0,publickey);
\r
338 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
339 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
340 st2.Bind(1,identityid);
\r
344 if(st2.RowReturned())
\r
346 if(st2.ResultNull(0)==false)
\r
348 st2.ResultInt(0,index);
\r
354 StringFunctions::Convert(index,indexstr);
\r
355 StringFunctions::Convert(identityid,identityidstr);
\r
357 message.SetName("ClientGet");
\r
358 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
359 message["Identifier"]="TrustListRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
360 message["ReturnType"]="direct";
\r
361 message["MaxSize"]="1000000"; // 1 MB
\r
363 m_fcp->SendMessage(message);
\r
365 m_requesting.push_back(identityid);
\r
369 m_ids[identityid]=true;
\r