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
58 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file
\r
59 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");
\r
60 st.Bind(0,identityid);
\r
64 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
65 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust) VALUES(?,?,?,?);");
\r
66 // loop through all trust entries in xml and add to database if we don't already know them
\r
67 for(long i=0; i<xml.TrustCount(); i++)
\r
70 std::string identity;
\r
71 identity=xml.GetIdentity(i);
\r
73 st.Bind(0,identity);
\r
75 if(st.RowReturned()==false)
\r
77 m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);
\r
85 //insert trust for this identity
\r
86 trustst.Bind(0,identityid);
\r
88 if(xml.GetMessageTrust(i)==-1)
\r
94 trustst.Bind(2,xml.GetMessageTrust(i));
\r
96 if(xml.GetTrustListTrust(i)==-1)
\r
102 trustst.Bind(3,xml.GetTrustListTrust(i));
\r
108 trustst.Finalize();
\r
111 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
112 st.Bind(0,identityid);
\r
113 st.Bind(1,idparts[4]);
\r
118 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);
\r
122 // bad data - mark index
\r
123 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
124 st.Bind(0,identityid);
\r
125 st.Bind(1,idparts[4]);
\r
130 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);
\r
133 // remove this identityid from request list
\r
134 RemoveFromRequestList(identityid);
\r
140 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
\r
143 SQLite3DB::Statement st;
\r
144 std::vector<std::string> idparts;
\r
149 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
150 StringFunctions::Convert(idparts[1],identityid);
\r
151 StringFunctions::Convert(idparts[2],index);
\r
153 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
154 if(message["Fatal"]=="true")
\r
156 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
157 st.Bind(0,identityid);
\r
158 st.Bind(1,idparts[4]);
\r
163 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
166 // remove this identityid from request list
\r
167 RemoveFromRequestList(identityid);
\r
173 void TrustListRequester::Initialize()
\r
175 std::string tempval="";
\r
176 m_fcpuniquename="TrustListRequester";
\r
177 Option::Instance()->Get("MaxIdentityRequests",tempval);
\r
178 StringFunctions::Convert(tempval,m_maxrequests);
\r
179 if(m_maxrequests<1)
\r
182 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
184 if(m_maxrequests>100)
\r
186 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
188 m_tempdate.SetToGMTime();
\r
191 void TrustListRequester::PopulateIDList()
\r
197 date.SetToGMTime();
\r
199 // 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
200 sql="SELECT IdentityID FROM tblIdentity ";
\r
201 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
202 sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";
\r
204 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
209 while(st.RowReturned())
\r
211 st.ResultInt(0,id);
\r
217 void TrustListRequester::StartRequest(const long &identityid)
\r
220 FCPMessage message;
\r
221 std::string publickey;
\r
223 std::string indexstr;
\r
224 std::string identityidstr;
\r
226 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
227 st.Bind(0,identityid);
\r
230 if(st.RowReturned())
\r
232 st.ResultText(0,publickey);
\r
236 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");
\r
237 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
238 st2.Bind(1,identityid);
\r
242 if(st2.RowReturned())
\r
244 if(st2.ResultNull(0)==false)
\r
246 st2.ResultInt(0,index);
\r
252 StringFunctions::Convert(index,indexstr);
\r
253 StringFunctions::Convert(identityid,identityidstr);
\r
255 message.SetName("ClientGet");
\r
256 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
257 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
258 message["ReturnType"]="direct";
\r
259 message["MaxSize"]="1000000"; // 1 MB
\r
261 m_fcp->SendMessage(message);
\r
263 m_requesting.push_back(identityid);
\r
267 m_ids[identityid]=true;
\r