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
6 #include <Poco/DateTimeFormatter.h>
\r
12 TrustListRequester::TrustListRequester()
\r
17 TrustListRequester::TrustListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)
\r
22 const bool TrustListRequester::HandleAllData(FCPMessage &message)
\r
25 SQLite3DB::Statement st;
\r
26 SQLite3DB::Statement trustst;
\r
27 std::vector<std::string> idparts;
\r
29 std::vector<char> data;
\r
34 int dayinsertcount=0;
\r
36 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
37 StringFunctions::Convert(message["DataLength"],datalength);
\r
38 StringFunctions::Convert(idparts[1],identityid);
\r
39 StringFunctions::Convert(idparts[2],index);
\r
41 // wait for all data to be received from connection
\r
42 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
47 // if we got disconnected- return immediately
\r
48 if(m_fcp->Connected()==false)
\r
54 data.resize(datalength);
\r
55 m_fcp->ReceiveRaw(&data[0],datalength);
\r
57 // get count of identities added in last 24 hours
\r
58 st=m_db->Prepare("SELECT COUNT(*) FROM tblIdentity WHERE DateAdded>=?;");
\r
59 st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
61 if(st.RowReturned())
\r
63 if(st.ResultNull(0)==false)
\r
65 st.ResultInt(0,dayinsertcount);
\r
70 m_log->error("TrustListRequester::HandleAllData couldn't get count of identities added in last 24 hours");
\r
73 // parse file into xml and update the database
\r
74 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
76 // find the identity name and public key of the identity publishing the trust list
\r
77 std::string publisherid="";
\r
78 st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
79 st.Bind(0,identityid);
\r
81 if(st.RowReturned())
\r
83 std::string publishername="";
\r
84 std::string publisherpublickey="";
\r
85 st.ResultText(0,publishername);
\r
86 st.ResultText(1,publisherpublickey);
\r
87 publisherid=publishername;
\r
88 if(publisherpublickey.size()>4)
\r
90 publisherid+=publisherpublickey.substr(3,44);
\r
95 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file
\r
96 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");
\r
97 st.Bind(0,identityid);
\r
101 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
102 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust,MessageTrustComment,TrustListTrustComment) VALUES(?,?,?,?,?,?);");
\r
104 SQLite3DB::Statement idinsert=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES(?,?,?);");
\r
106 // loop through all trust entries in xml and add to database if we don't already know them
\r
107 for(long i=0; i<xml.TrustCount(); i++)
\r
110 std::string identity;
\r
111 std::string messagetrustcomment="";
\r
112 std::string trustlisttrustcomment="";
\r
113 identity=xml.GetIdentity(i);
\r
115 st.Bind(0,identity);
\r
117 if(st.RowReturned()==false)
\r
119 if(insertcount<50 && dayinsertcount<100)
\r
121 idinsert.Bind(0,identity);
\r
122 idinsert.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
123 idinsert.Bind(2,"trust list of "+publisherid);
\r
124 idinsert.Step(true);
\r
125 id=idinsert.GetLastInsertRowID();
\r
133 st.ResultInt(0,id);
\r
137 //insert trust for this identity
\r
140 trustst.Bind(0,identityid);
\r
141 trustst.Bind(1,id);
\r
142 if(xml.GetMessageTrust(i)==-1)
\r
148 trustst.Bind(2,xml.GetMessageTrust(i));
\r
150 if(xml.GetTrustListTrust(i)==-1)
\r
156 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
158 messagetrustcomment=xml.GetMessageTrustComment(i);
\r
159 trustlisttrustcomment=xml.GetTrustListTrustComment(i);
\r
160 // limit comments to 50 characters each
\r
161 if(messagetrustcomment.size()>50)
\r
163 messagetrustcomment.erase(50);
\r
165 if(trustlisttrustcomment.size()>50)
\r
167 trustlisttrustcomment.erase(50);
\r
169 trustst.Bind(4,messagetrustcomment);
\r
170 trustst.Bind(5,trustlisttrustcomment);
\r
176 trustst.Finalize();
\r
179 if(insertcount>=50)
\r
181 m_log->warning("TrustListRequester::HandleAllData TrustList contained more than 50 new identities : "+message["Identifier"]);
\r
183 if(dayinsertcount>=100)
\r
185 m_log->warning("TrustListRequester::HandleAllData TrustList would have inserted more than 100 new identities in the last 24 hours : "+message["Identifier"]);
\r
188 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
189 st.Bind(0,identityid);
\r
190 st.Bind(1,idparts[4]);
\r
195 m_log->debug("TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
199 // bad data - mark index
\r
200 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
201 st.Bind(0,identityid);
\r
202 st.Bind(1,idparts[4]);
\r
207 m_log->error("TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
210 // remove this identityid from request list
\r
211 RemoveFromRequestList(identityid);
\r
217 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
219 SQLite3DB::Statement st;
\r
220 std::vector<std::string> idparts;
\r
224 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
225 StringFunctions::Convert(idparts[1],identityid);
\r
226 StringFunctions::Convert(idparts[2],index);
\r
228 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
229 if(message["Fatal"]=="true")
\r
231 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
232 st.Bind(0,identityid);
\r
233 st.Bind(1,idparts[4]);
\r
238 m_log->error("TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
241 // remove this identityid from request list
\r
242 RemoveFromRequestList(identityid);
\r
248 void TrustListRequester::Initialize()
\r
250 std::string tempval="";
\r
251 m_fcpuniquename="TrustListRequester";
\r
252 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
253 StringFunctions::Convert(tempval,m_maxrequests);
\r
254 if(m_maxrequests<1)
\r
257 m_log->error("Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
259 if(m_maxrequests>100)
\r
261 m_log->warning("Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
263 m_tempdate=Poco::Timestamp();
\r
266 void TrustListRequester::PopulateIDList()
\r
268 Poco::DateTime date;
\r
272 // 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
273 sql="SELECT IdentityID FROM tblIdentity ";
\r
274 sql+="WHERE Name IS NOT NULL AND Name <> '' AND PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+Poco::DateTimeFormatter::format(date,"%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
275 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
277 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
282 while(st.RowReturned())
\r
284 st.ResultInt(0,id);
\r
290 void TrustListRequester::StartRequest(const long &identityid)
\r
292 Poco::DateTime now;
\r
293 FCPMessage message;
\r
294 std::string publickey;
\r
296 std::string indexstr;
\r
297 std::string identityidstr;
\r
299 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
300 st.Bind(0,identityid);
\r
303 if(st.RowReturned())
\r
305 st.ResultText(0,publickey);
\r
307 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
308 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
309 st2.Bind(1,identityid);
\r
313 if(st2.RowReturned())
\r
315 if(st2.ResultNull(0)==false)
\r
317 st2.ResultInt(0,index);
\r
323 StringFunctions::Convert(index,indexstr);
\r
324 StringFunctions::Convert(identityid,identityidstr);
\r
326 message.SetName("ClientGet");
\r
327 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
328 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
329 message["ReturnType"]="direct";
\r
330 message["MaxSize"]="1000000"; // 1 MB
\r
332 m_fcp->SendMessage(message);
\r
334 m_requesting.push_back(identityid);
\r
338 m_ids[identityid]=true;
\r