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 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
35 StringFunctions::Convert(message["DataLength"],datalength);
\r
36 StringFunctions::Convert(idparts[1],identityid);
\r
37 StringFunctions::Convert(idparts[2],index);
\r
39 // wait for all data to be received from connection
\r
40 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
45 // if we got disconnected- return immediately
\r
46 if(m_fcp->Connected()==false)
\r
52 data.resize(datalength);
\r
53 m_fcp->ReceiveRaw(&data[0],datalength);
\r
55 // parse file into xml and update the database
\r
56 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
58 // find the identity name and public key of the identity publishing the trust list
\r
59 std::string publisherid="";
\r
60 st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
61 st.Bind(0,identityid);
\r
63 if(st.RowReturned())
\r
65 std::string publishername="";
\r
66 std::string publisherpublickey="";
\r
67 st.ResultText(0,publishername);
\r
68 st.ResultText(1,publisherpublickey);
\r
69 publisherid=publishername;
\r
70 if(publisherpublickey.size()>4)
\r
72 publisherid+=publisherpublickey.substr(3,44);
\r
77 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file
\r
78 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");
\r
79 st.Bind(0,identityid);
\r
83 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
84 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust,MessageTrustComment,TrustListTrustComment) VALUES(?,?,?,?,?,?);");
\r
86 SQLite3DB::Statement idinsert=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES(?,?,?);");
\r
88 // loop through all trust entries in xml and add to database if we don't already know them
\r
89 for(long i=0; i<xml.TrustCount(); i++)
\r
92 std::string identity;
\r
93 std::string messagetrustcomment="";
\r
94 std::string trustlisttrustcomment="";
\r
95 identity=xml.GetIdentity(i);
\r
97 st.Bind(0,identity);
\r
99 if(st.RowReturned()==false)
\r
101 idinsert.Bind(0,identity);
\r
102 idinsert.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
103 idinsert.Bind(2,"trust list of "+publisherid);
\r
104 idinsert.Step(true);
\r
105 id=idinsert.GetLastInsertRowID();
\r
107 // m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);
\r
111 st.ResultInt(0,id);
\r
115 //insert trust for this identity
\r
116 trustst.Bind(0,identityid);
\r
117 trustst.Bind(1,id);
\r
118 if(xml.GetMessageTrust(i)==-1)
\r
124 trustst.Bind(2,xml.GetMessageTrust(i));
\r
126 if(xml.GetTrustListTrust(i)==-1)
\r
132 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
134 messagetrustcomment=xml.GetMessageTrustComment(i);
\r
135 trustlisttrustcomment=xml.GetTrustListTrustComment(i);
\r
136 // limit comments to 50 characters each
\r
137 if(messagetrustcomment.size()>50)
\r
139 messagetrustcomment.erase(50);
\r
141 if(trustlisttrustcomment.size()>50)
\r
143 trustlisttrustcomment.erase(50);
\r
145 trustst.Bind(4,messagetrustcomment);
\r
146 trustst.Bind(5,trustlisttrustcomment);
\r
151 trustst.Finalize();
\r
154 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
155 st.Bind(0,identityid);
\r
156 st.Bind(1,idparts[4]);
\r
161 m_log->debug("TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
165 // bad data - mark index
\r
166 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
167 st.Bind(0,identityid);
\r
168 st.Bind(1,idparts[4]);
\r
173 m_log->error("TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
176 // remove this identityid from request list
\r
177 RemoveFromRequestList(identityid);
\r
183 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
185 SQLite3DB::Statement st;
\r
186 std::vector<std::string> idparts;
\r
190 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
191 StringFunctions::Convert(idparts[1],identityid);
\r
192 StringFunctions::Convert(idparts[2],index);
\r
194 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
195 if(message["Fatal"]=="true")
\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::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
207 // remove this identityid from request list
\r
208 RemoveFromRequestList(identityid);
\r
214 void TrustListRequester::Initialize()
\r
216 std::string tempval="";
\r
217 m_fcpuniquename="TrustListRequester";
\r
218 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
219 StringFunctions::Convert(tempval,m_maxrequests);
\r
220 if(m_maxrequests<1)
\r
223 m_log->error("Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
225 if(m_maxrequests>100)
\r
227 m_log->warning("Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
229 m_tempdate=Poco::Timestamp();
\r
232 void TrustListRequester::PopulateIDList()
\r
234 Poco::DateTime date;
\r
238 // 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
239 sql="SELECT IdentityID FROM tblIdentity ";
\r
240 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
241 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
243 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
248 while(st.RowReturned())
\r
250 st.ResultInt(0,id);
\r
256 void TrustListRequester::StartRequest(const long &identityid)
\r
258 Poco::DateTime now;
\r
259 FCPMessage message;
\r
260 std::string publickey;
\r
262 std::string indexstr;
\r
263 std::string identityidstr;
\r
265 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
266 st.Bind(0,identityid);
\r
269 if(st.RowReturned())
\r
271 st.ResultText(0,publickey);
\r
273 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
274 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
275 st2.Bind(1,identityid);
\r
279 if(st2.RowReturned())
\r
281 if(st2.ResultNull(0)==false)
\r
283 st2.ResultInt(0,index);
\r
289 StringFunctions::Convert(index,indexstr);
\r
290 StringFunctions::Convert(identityid,identityidstr);
\r
292 message.SetName("ClientGet");
\r
293 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
294 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
295 message["ReturnType"]="direct";
\r
296 message["MaxSize"]="1000000"; // 1 MB
\r
298 m_fcp->SendMessage(message);
\r
300 m_requesting.push_back(identityid);
\r
304 m_ids[identityid]=true;
\r