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
7 #include <Poco/Timespan.h>
\r
13 TrustListRequester::TrustListRequester()
\r
18 TrustListRequester::TrustListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)
\r
23 const bool TrustListRequester::HandleAllData(FCPMessage &message)
\r
26 SQLite3DB::Statement st;
\r
27 SQLite3DB::Statement trustst;
\r
28 std::vector<std::string> idparts;
\r
30 std::vector<char> data;
\r
35 int dayinsertcount=0;
\r
36 int previnsertcount=0;
\r
38 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
39 StringFunctions::Convert(message["DataLength"],datalength);
\r
40 StringFunctions::Convert(idparts[1],identityid);
\r
41 StringFunctions::Convert(idparts[2],index);
\r
43 // wait for all data to be received from connection
\r
44 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
49 // if we got disconnected- return immediately
\r
50 if(m_fcp->Connected()==false)
\r
56 data.resize(datalength);
\r
59 m_fcp->ReceiveRaw(&data[0],datalength);
\r
62 // get count of identities added in last 24 hours
\r
63 st=m_db->Prepare("SELECT COUNT(*) FROM tblIdentity WHERE DateAdded>=?;");
\r
64 now-=Poco::Timespan(1,0,0,0,0);
\r
65 st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
67 if(st.RowReturned())
\r
69 if(st.ResultNull(0)==false)
\r
71 st.ResultInt(0,dayinsertcount);
\r
76 m_log->error("TrustListRequester::HandleAllData couldn't get count of identities added in last 24 hours");
\r
79 // get count of identities added more than 24 hours ago - if 0 then we will accept more than 100 identities now
\r
80 st=m_db->Prepare("SELECT COUNT(*) FROM tblIdentity WHERE DateAdded<?;");
\r
81 st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
83 if(st.RowReturned())
\r
85 if(st.ResultNull(0)==false)
\r
87 st.ResultInt(0,previnsertcount);
\r
92 m_log->error("TrustListRequester::HandleAllData couldn't get count of identities added more than 24 hours ago");
\r
95 now=Poco::DateTime();
\r
97 // parse file into xml and update the database
\r
98 if(data.size()>0 && xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
100 // find the identity name and public key of the identity publishing the trust list
\r
101 std::string publisherid="";
\r
102 st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
103 st.Bind(0,identityid);
\r
105 if(st.RowReturned())
\r
107 std::string publishername="";
\r
108 std::string publisherpublickey="";
\r
109 st.ResultText(0,publishername);
\r
110 st.ResultText(1,publisherpublickey);
\r
111 publisherid=publishername;
\r
112 if(publisherpublickey.size()>4)
\r
114 publisherid+=publisherpublickey.substr(3,44);
\r
119 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file
\r
120 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");
\r
121 st.Bind(0,identityid);
\r
125 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
126 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust,MessageTrustComment,TrustListTrustComment) VALUES(?,?,?,?,?,?);");
\r
128 SQLite3DB::Statement idinsert=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES(?,?,?);");
\r
130 // loop through all trust entries in xml and add to database if we don't already know them
\r
131 for(long i=0; i<xml.TrustCount(); i++)
\r
134 std::string identity;
\r
135 std::string messagetrustcomment="";
\r
136 std::string trustlisttrustcomment="";
\r
137 identity=xml.GetIdentity(i);
\r
139 st.Bind(0,identity);
\r
141 if(st.RowReturned()==false)
\r
143 if(insertcount<50 && (dayinsertcount<100 || previnsertcount==0))
\r
145 idinsert.Bind(0,identity);
\r
146 idinsert.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
147 idinsert.Bind(2,"trust list of "+publisherid);
\r
148 idinsert.Step(true);
\r
149 id=idinsert.GetLastInsertRowID();
\r
157 st.ResultInt(0,id);
\r
161 //insert trust for this identity
\r
164 trustst.Bind(0,identityid);
\r
165 trustst.Bind(1,id);
\r
166 if(xml.GetMessageTrust(i)==-1)
\r
172 trustst.Bind(2,xml.GetMessageTrust(i));
\r
174 if(xml.GetTrustListTrust(i)==-1)
\r
180 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
182 messagetrustcomment=xml.GetMessageTrustComment(i);
\r
183 trustlisttrustcomment=xml.GetTrustListTrustComment(i);
\r
184 // limit comments to 50 characters each
\r
185 if(messagetrustcomment.size()>50)
\r
187 messagetrustcomment.erase(50);
\r
189 if(trustlisttrustcomment.size()>50)
\r
191 trustlisttrustcomment.erase(50);
\r
193 trustst.Bind(4,messagetrustcomment);
\r
194 trustst.Bind(5,trustlisttrustcomment);
\r
200 trustst.Finalize();
\r
203 if(insertcount>=50)
\r
205 m_log->warning("TrustListRequester::HandleAllData TrustList contained more than 50 new identities : "+message["Identifier"]);
\r
207 if(dayinsertcount>=100 && previnsertcount>0)
\r
209 m_log->warning("TrustListRequester::HandleAllData TrustList would have inserted more than 100 new identities in the last 24 hours : "+message["Identifier"]);
\r
212 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
213 st.Bind(0,identityid);
\r
214 st.Bind(1,idparts[4]);
\r
219 m_log->debug("TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
223 // bad data - mark index
\r
224 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
225 st.Bind(0,identityid);
\r
226 st.Bind(1,idparts[4]);
\r
231 m_log->error("TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
234 // remove this identityid from request list
\r
235 RemoveFromRequestList(identityid);
\r
241 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
243 SQLite3DB::Statement st;
\r
244 std::vector<std::string> idparts;
\r
248 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
249 StringFunctions::Convert(idparts[1],identityid);
\r
250 StringFunctions::Convert(idparts[2],index);
\r
252 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
253 if(message["Fatal"]=="true")
\r
255 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
256 st.Bind(0,identityid);
\r
257 st.Bind(1,idparts[4]);
\r
262 m_log->error("TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
265 // remove this identityid from request list
\r
266 RemoveFromRequestList(identityid);
\r
272 void TrustListRequester::Initialize()
\r
274 std::string tempval="";
\r
275 m_fcpuniquename="TrustListRequester";
\r
278 Option::Instance()->GetInt("MaxIdentityRequests",m_maxrequests);
\r
279 if(m_maxrequests<1)
\r
282 m_log->error("Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
284 if(m_maxrequests>100)
\r
286 m_log->warning("Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
288 m_tempdate=Poco::Timestamp();
\r
291 void TrustListRequester::PopulateIDList()
\r
293 Poco::DateTime date;
\r
297 // 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
298 sql="SELECT IdentityID FROM tblIdentity ";
\r
299 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
300 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
302 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
307 while(st.RowReturned())
\r
309 st.ResultInt(0,id);
\r
315 void TrustListRequester::StartRequest(const long &identityid)
\r
317 Poco::DateTime now;
\r
318 FCPMessage message;
\r
319 std::string publickey;
\r
321 std::string indexstr;
\r
322 std::string identityidstr;
\r
324 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
325 st.Bind(0,identityid);
\r
328 if(st.RowReturned())
\r
330 st.ResultText(0,publickey);
\r
332 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
333 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
334 st2.Bind(1,identityid);
\r
338 if(st2.RowReturned())
\r
340 if(st2.ResultNull(0)==false)
\r
342 st2.ResultInt(0,index);
\r
348 StringFunctions::Convert(index,indexstr);
\r
349 StringFunctions::Convert(identityid,identityidstr);
\r
351 message.SetName("ClientGet");
\r
352 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
353 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
354 message["ReturnType"]="direct";
\r
355 message["MaxSize"]="1000000"; // 1 MB
\r
357 m_fcp->SendMessage(message);
\r
359 m_requesting.push_back(identityid);
\r
363 m_ids[identityid]=true;
\r