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
138 trustst.Bind(0,identityid);
\r
139 trustst.Bind(1,id);
\r
140 if(xml.GetMessageTrust(i)==-1)
\r
146 trustst.Bind(2,xml.GetMessageTrust(i));
\r
148 if(xml.GetTrustListTrust(i)==-1)
\r
154 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
156 messagetrustcomment=xml.GetMessageTrustComment(i);
\r
157 trustlisttrustcomment=xml.GetTrustListTrustComment(i);
\r
158 // limit comments to 50 characters each
\r
159 if(messagetrustcomment.size()>50)
\r
161 messagetrustcomment.erase(50);
\r
163 if(trustlisttrustcomment.size()>50)
\r
165 trustlisttrustcomment.erase(50);
\r
167 trustst.Bind(4,messagetrustcomment);
\r
168 trustst.Bind(5,trustlisttrustcomment);
\r
173 trustst.Finalize();
\r
176 if(insertcount>=50)
\r
178 m_log->warning("TrustListRequester::HandleAllData TrustList contained more than 50 new identities : "+message["Identifier"]);
\r
180 if(dayinsertcount>=100)
\r
182 m_log->warning("TrustListRequester::HandleAllData TrustList would have inserted more than 100 new identities in the last 24 hours : "+message["Identifier"]);
\r
185 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
186 st.Bind(0,identityid);
\r
187 st.Bind(1,idparts[4]);
\r
192 m_log->debug("TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
196 // bad data - mark index
\r
197 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
198 st.Bind(0,identityid);
\r
199 st.Bind(1,idparts[4]);
\r
204 m_log->error("TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
207 // remove this identityid from request list
\r
208 RemoveFromRequestList(identityid);
\r
214 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
216 SQLite3DB::Statement st;
\r
217 std::vector<std::string> idparts;
\r
221 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
222 StringFunctions::Convert(idparts[1],identityid);
\r
223 StringFunctions::Convert(idparts[2],index);
\r
225 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
226 if(message["Fatal"]=="true")
\r
228 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
229 st.Bind(0,identityid);
\r
230 st.Bind(1,idparts[4]);
\r
235 m_log->error("TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
238 // remove this identityid from request list
\r
239 RemoveFromRequestList(identityid);
\r
245 void TrustListRequester::Initialize()
\r
247 std::string tempval="";
\r
248 m_fcpuniquename="TrustListRequester";
\r
249 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
250 StringFunctions::Convert(tempval,m_maxrequests);
\r
251 if(m_maxrequests<1)
\r
254 m_log->error("Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
256 if(m_maxrequests>100)
\r
258 m_log->warning("Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
260 m_tempdate=Poco::Timestamp();
\r
263 void TrustListRequester::PopulateIDList()
\r
265 Poco::DateTime date;
\r
269 // 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
270 sql="SELECT IdentityID FROM tblIdentity ";
\r
271 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
272 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
274 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
279 while(st.RowReturned())
\r
281 st.ResultInt(0,id);
\r
287 void TrustListRequester::StartRequest(const long &identityid)
\r
289 Poco::DateTime now;
\r
290 FCPMessage message;
\r
291 std::string publickey;
\r
293 std::string indexstr;
\r
294 std::string identityidstr;
\r
296 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
297 st.Bind(0,identityid);
\r
300 if(st.RowReturned())
\r
302 st.ResultText(0,publickey);
\r
304 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
305 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
306 st2.Bind(1,identityid);
\r
310 if(st2.RowReturned())
\r
312 if(st2.ResultNull(0)==false)
\r
314 st2.ResultInt(0,index);
\r
320 StringFunctions::Convert(index,indexstr);
\r
321 StringFunctions::Convert(identityid,identityidstr);
\r
323 message.SetName("ClientGet");
\r
324 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
325 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
326 message["ReturnType"]="direct";
\r
327 message["MaxSize"]="1000000"; // 1 MB
\r
329 m_fcp->SendMessage(message);
\r
331 m_requesting.push_back(identityid);
\r
335 m_ids[identityid]=true;
\r