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):IIndexRequester<long>(fcp)
\r
20 const bool TrustListRequester::HandleAllData(FCPMessage &message)
\r
23 SQLite3DB::Statement st;
\r
24 SQLite3DB::Statement trustst;
\r
25 std::vector<std::string> idparts;
\r
27 std::vector<char> data;
\r
33 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
34 StringFunctions::Convert(message["DataLength"],datalength);
\r
35 StringFunctions::Convert(idparts[1],identityid);
\r
36 StringFunctions::Convert(idparts[2],index);
\r
38 // wait for all data to be received from connection
\r
39 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
44 // if we got disconnected- return immediately
\r
45 if(m_fcp->Connected()==false)
\r
51 data.resize(datalength);
\r
52 m_fcp->ReceiveRaw(&data[0],datalength);
\r
54 // parse file into xml and update the database
\r
55 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
57 // find the identity name and public key of the identity publishing the trust list
\r
58 std::string publisherid="";
\r
59 st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
60 st.Bind(0,identityid);
\r
62 if(st.RowReturned())
\r
64 std::string publishername="";
\r
65 std::string publisherpublickey="";
\r
66 st.ResultText(0,publishername);
\r
67 st.ResultText(1,publisherpublickey);
\r
68 publisherid=publishername;
\r
69 if(publisherpublickey.size()>4)
\r
71 publisherid+=publisherpublickey.substr(3,44);
\r
76 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file
\r
77 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");
\r
78 st.Bind(0,identityid);
\r
82 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
83 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust,MessageTrustComment,TrustListTrustComment) VALUES(?,?,?,?,?,?);");
\r
85 SQLite3DB::Statement idinsert=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES(?,?,?);");
\r
87 // loop through all trust entries in xml and add to database if we don't already know them
\r
88 for(long i=0; i<xml.TrustCount(); i++)
\r
91 std::string identity;
\r
92 std::string messagetrustcomment="";
\r
93 std::string trustlisttrustcomment="";
\r
94 identity=xml.GetIdentity(i);
\r
96 st.Bind(0,identity);
\r
98 if(st.RowReturned()==false)
\r
100 idinsert.Bind(0,identity);
\r
101 idinsert.Bind(1,now.Format("%Y-%m-%d %H:%M:%S"));
\r
102 idinsert.Bind(2,"trust list of "+publisherid);
\r
103 idinsert.Step(true);
\r
104 id=idinsert.GetLastInsertRowID();
\r
106 // m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);
\r
110 st.ResultInt(0,id);
\r
114 //insert trust for this identity
\r
115 trustst.Bind(0,identityid);
\r
116 trustst.Bind(1,id);
\r
117 if(xml.GetMessageTrust(i)==-1)
\r
123 trustst.Bind(2,xml.GetMessageTrust(i));
\r
125 if(xml.GetTrustListTrust(i)==-1)
\r
131 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
133 messagetrustcomment=xml.GetMessageTrustComment(i);
\r
134 trustlisttrustcomment=xml.GetTrustListTrustComment(i);
\r
135 // limit comments to 50 characters each
\r
136 if(messagetrustcomment.size()>50)
\r
138 messagetrustcomment.erase(50);
\r
140 if(trustlisttrustcomment.size()>50)
\r
142 trustlisttrustcomment.erase(50);
\r
144 trustst.Bind(4,messagetrustcomment);
\r
145 trustst.Bind(5,trustlisttrustcomment);
\r
150 trustst.Finalize();
\r
153 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
154 st.Bind(0,identityid);
\r
155 st.Bind(1,idparts[4]);
\r
160 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
164 // bad data - mark index
\r
165 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
166 st.Bind(0,identityid);
\r
167 st.Bind(1,idparts[4]);
\r
172 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
175 // remove this identityid from request list
\r
176 RemoveFromRequestList(identityid);
\r
182 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
185 SQLite3DB::Statement st;
\r
186 std::vector<std::string> idparts;
\r
191 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
192 StringFunctions::Convert(idparts[1],identityid);
\r
193 StringFunctions::Convert(idparts[2],index);
\r
195 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
196 if(message["Fatal"]=="true")
\r
198 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
199 st.Bind(0,identityid);
\r
200 st.Bind(1,idparts[4]);
\r
205 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
208 // remove this identityid from request list
\r
209 RemoveFromRequestList(identityid);
\r
215 void TrustListRequester::Initialize()
\r
217 std::string tempval="";
\r
218 m_fcpuniquename="TrustListRequester";
\r
219 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
220 StringFunctions::Convert(tempval,m_maxrequests);
\r
221 if(m_maxrequests<1)
\r
224 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
226 if(m_maxrequests>100)
\r
228 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
230 m_tempdate.SetToGMTime();
\r
233 void TrustListRequester::PopulateIDList()
\r
239 date.SetToGMTime();
\r
241 // 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
242 sql="SELECT IdentityID FROM tblIdentity ";
\r
243 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
244 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
246 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
251 while(st.RowReturned())
\r
253 st.ResultInt(0,id);
\r
259 void TrustListRequester::StartRequest(const long &identityid)
\r
262 FCPMessage message;
\r
263 std::string publickey;
\r
265 std::string indexstr;
\r
266 std::string identityidstr;
\r
268 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
269 st.Bind(0,identityid);
\r
272 if(st.RowReturned())
\r
274 st.ResultText(0,publickey);
\r
278 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
279 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
280 st2.Bind(1,identityid);
\r
284 if(st2.RowReturned())
\r
286 if(st2.ResultNull(0)==false)
\r
288 st2.ResultInt(0,index);
\r
294 StringFunctions::Convert(index,indexstr);
\r
295 StringFunctions::Convert(identityid,identityidstr);
\r
297 message.SetName("ClientGet");
\r
298 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
299 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
300 message["ReturnType"]="direct";
\r
301 message["MaxSize"]="1000000"; // 1 MB
\r
303 m_fcp->SendMessage(message);
\r
305 m_requesting.push_back(identityid);
\r
309 m_ids[identityid]=true;
\r