version 0.1.13
[fms.git] / src / freenet / trustlistrequester.cpp
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
5 \r
6 #ifdef XMEM\r
7         #include <xmem.h>\r
8 #endif\r
9 \r
10 TrustListRequester::TrustListRequester()\r
11 {\r
12         Initialize();\r
13 }\r
14 \r
15 TrustListRequester::TrustListRequester(FCPv2 *fcp):IFCPConnected(fcp)\r
16 {\r
17         Initialize();\r
18 }\r
19 \r
20 void TrustListRequester::FCPConnected()\r
21 {\r
22         m_requesting.clear();\r
23         PopulateIDList();\r
24 }\r
25 \r
26 void TrustListRequester::FCPDisconnected()\r
27 {\r
28         \r
29 }\r
30 \r
31 const bool TrustListRequester::HandleAllData(FCPMessage &message)\r
32 {\r
33         DateTime now;\r
34         SQLite3DB::Statement st;\r
35         SQLite3DB::Statement trustst;\r
36         std::vector<std::string> idparts;\r
37         long datalength;\r
38         std::vector<char> data;\r
39         TrustListXML xml;\r
40         long identityid;\r
41         long index;\r
42 \r
43         now.SetToGMTime();\r
44         StringFunctions::Split(message["Identifier"],"|",idparts);\r
45         StringFunctions::Convert(message["DataLength"],datalength);\r
46         StringFunctions::Convert(idparts[1],identityid);\r
47         StringFunctions::Convert(idparts[2],index);\r
48 \r
49         // wait for all data to be received from connection\r
50         while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
51         {\r
52                 m_fcp->Update(1);\r
53         }\r
54 \r
55         // if we got disconnected- return immediately\r
56         if(m_fcp->Connected()==false)\r
57         {\r
58                 return false;\r
59         }\r
60 \r
61         // receive the file\r
62         data.resize(datalength);\r
63         m_fcp->ReceiveRaw(&data[0],datalength);\r
64 \r
65         // parse file into xml and update the database\r
66         if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
67         {\r
68 \r
69                 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file\r
70                 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");\r
71                 st.Bind(0,identityid);\r
72                 st.Step();\r
73                 st.Finalize();\r
74 \r
75                 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");\r
76                 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust) VALUES(?,?,?,?);");\r
77                 // loop through all trust entries in xml and add to database if we don't already know them\r
78                 for(long i=0; i<xml.TrustCount(); i++)\r
79                 {\r
80                         int id;\r
81                         std::string identity;\r
82                         identity=xml.GetIdentity(i);\r
83 \r
84                         st.Bind(0,identity);\r
85                         st.Step();\r
86                         if(st.RowReturned()==false)\r
87                         {\r
88                                 m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);\r
89                         }\r
90                         else\r
91                         {\r
92                                 st.ResultInt(0,id);\r
93                         }\r
94                         st.Reset();\r
95 \r
96                         //insert trust for this identity\r
97                         trustst.Bind(0,identityid);\r
98                         trustst.Bind(1,id);\r
99                         if(xml.GetMessageTrust(i)==-1)\r
100                         {\r
101                                 trustst.Bind(2);\r
102                         }\r
103                         else\r
104                         {\r
105                                 trustst.Bind(2,xml.GetMessageTrust(i));\r
106                         }\r
107                         if(xml.GetTrustListTrust(i)==-1)\r
108                         {\r
109                                 trustst.Bind(3);\r
110                         }\r
111                         else\r
112                         {\r
113                                 trustst.Bind(3,xml.GetTrustListTrust(i));\r
114                         }\r
115                         trustst.Step();\r
116                         trustst.Reset();\r
117 \r
118                 }\r
119                 trustst.Finalize();\r
120                 st.Finalize();\r
121 \r
122                 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");\r
123                 st.Bind(0,identityid);\r
124                 st.Bind(1,idparts[4]);\r
125                 st.Bind(2,index);\r
126                 st.Step();\r
127                 st.Finalize();\r
128 \r
129                 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);\r
130         }\r
131         else\r
132         {\r
133                 // bad data - mark index\r
134                 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
135                 st.Bind(0,identityid);\r
136                 st.Bind(1,idparts[4]);\r
137                 st.Bind(2,index);\r
138                 st.Step();\r
139                 st.Finalize();\r
140 \r
141                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);\r
142         }\r
143 \r
144         // remove this identityid from request list\r
145         RemoveFromRequestList(identityid);\r
146 \r
147         return true;\r
148 \r
149 }\r
150 \r
151 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)\r
152 {\r
153         DateTime now;\r
154         SQLite3DB::Statement st;\r
155         std::vector<std::string> idparts;\r
156         long identityid;\r
157         long index;\r
158 \r
159         now.SetToGMTime();\r
160         StringFunctions::Split(message["Identifier"],"|",idparts);\r
161         StringFunctions::Convert(idparts[1],identityid);\r
162         StringFunctions::Convert(idparts[2],index);     \r
163 \r
164         // if this is a fatal error - insert index into database so we won't try to download this index again\r
165         if(message["Fatal"]=="true")\r
166         {\r
167                 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
168                 st.Bind(0,identityid);\r
169                 st.Bind(1,idparts[4]);\r
170                 st.Bind(2,index);\r
171                 st.Step();\r
172                 st.Finalize();\r
173 \r
174                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
175         }\r
176 \r
177         // remove this identityid from request list\r
178         RemoveFromRequestList(identityid);\r
179 \r
180         return true;\r
181 \r
182 }\r
183 \r
184 const bool TrustListRequester::HandleMessage(FCPMessage &message)\r
185 {\r
186 \r
187         if(message["Identifier"].find("TrustListRequester")==0)\r
188         {\r
189                 if(message.GetName()=="DataFound")\r
190                 {\r
191                         return true;\r
192                 }\r
193 \r
194                 if(message.GetName()=="AllData")\r
195                 {\r
196                         return HandleAllData(message);\r
197                 }\r
198 \r
199                 if(message.GetName()=="GetFailed")\r
200                 {\r
201                         return HandleGetFailed(message);\r
202                 }\r
203 \r
204                 if(message.GetName()=="IdentifierCollision")\r
205                 {\r
206                         // remove one of the ids from the requesting list\r
207                         long identityid=0;\r
208                         std::vector<std::string> idparts;\r
209                         StringFunctions::Split(message["Identifier"],"|",idparts);\r
210                         StringFunctions::Convert(idparts[1],identityid);\r
211                         RemoveFromRequestList(identityid);\r
212                         return true;\r
213                 }\r
214         }\r
215 \r
216         return false;\r
217 }\r
218 \r
219 void TrustListRequester::Initialize()\r
220 {\r
221         std::string tempval="";\r
222         Option::Instance()->Get("MaxIdentityRequests",tempval);\r
223         StringFunctions::Convert(tempval,m_maxrequests);\r
224         if(m_maxrequests<1)\r
225         {\r
226                 m_maxrequests=1;\r
227                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+".  It must be 1 or greater.");\r
228         }\r
229         if(m_maxrequests>100)\r
230         {\r
231                 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
232         }\r
233         Option::Instance()->Get("MessageBase",m_messagebase);\r
234         m_tempdate.SetToGMTime();\r
235 }\r
236 \r
237 void TrustListRequester::PopulateIDList()\r
238 {\r
239         DateTime date;\r
240         int id;\r
241         std::string sql;\r
242 \r
243         date.SetToGMTime();\r
244 \r
245         // 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
246         sql="SELECT IdentityID FROM tblIdentity ";\r
247         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
248         sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";\r
249 \r
250         SQLite3DB::Statement st=m_db->Prepare(sql);\r
251         st.Step();\r
252 \r
253         m_ids.clear();\r
254 \r
255         while(st.RowReturned())\r
256         {\r
257                 st.ResultInt(0,id);\r
258                 m_ids[id]=false;\r
259                 st.Step();\r
260         }\r
261 }\r
262 \r
263 void TrustListRequester::Process()\r
264 {\r
265         // max is the smaller of the config value or the total number of identities we will request from\r
266         long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;\r
267 \r
268         // try to keep up to max requests going\r
269         if(m_requesting.size()<max)\r
270         {\r
271                 std::map<long,bool>::iterator i=m_ids.begin();\r
272                 while(i!=m_ids.end() && (*i).second==true)\r
273                 {\r
274                         i++;\r
275                 }\r
276 \r
277                 if(i!=m_ids.end())\r
278                 {\r
279                         StartRequest((*i).first);\r
280                 }\r
281                 else\r
282                 {\r
283                         // we requested from all ids in the list, repopulate the list\r
284                         PopulateIDList();\r
285                 }\r
286         }\r
287         // special case - if there were 0 identities on the list when we started then we will never get a chance to repopulate the list\r
288         // this will recheck for ids every minute\r
289         DateTime now;\r
290         now.SetToGMTime();\r
291         if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))\r
292         {\r
293                 PopulateIDList();\r
294                 m_tempdate=now;\r
295         }\r
296 \r
297 }\r
298 \r
299 void TrustListRequester::RegisterWithThread(FreenetMasterThread *thread)\r
300 {\r
301         thread->RegisterFCPConnected(this);\r
302         thread->RegisterFCPMessageHandler(this);\r
303         thread->RegisterPeriodicProcessor(this);\r
304 }\r
305 \r
306 void TrustListRequester::RemoveFromRequestList(const long identityid)\r
307 {\r
308         std::vector<long>::iterator i=m_requesting.begin();\r
309         while(i!=m_requesting.end() && (*i)!=identityid)\r
310         {\r
311                 i++;\r
312         }\r
313         if(i!=m_requesting.end())\r
314         {\r
315                 m_requesting.erase(i);\r
316         }\r
317 }\r
318 \r
319 void TrustListRequester::StartRequest(const long identityid)\r
320 {\r
321         DateTime now;\r
322         FCPMessage message;\r
323         std::string publickey;\r
324         int index;\r
325         std::string indexstr;\r
326         std::string identityidstr;\r
327 \r
328         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
329         st.Bind(0,identityid);\r
330         st.Step();\r
331 \r
332         if(st.RowReturned())\r
333         {\r
334                 st.ResultText(0,publickey);\r
335 \r
336                 now.SetToGMTime();\r
337 \r
338                 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");\r
339                 st2.Bind(0,now.Format("%Y-%m-%d"));\r
340                 st2.Bind(1,identityid);\r
341                 st2.Step();\r
342 \r
343                 index=0;\r
344                 if(st2.RowReturned())\r
345                 {\r
346                         if(st2.ResultNull(0)==false)\r
347                         {\r
348                                 st2.ResultInt(0,index);\r
349                                 index++;\r
350                         }\r
351                 }\r
352                 st2.Finalize();\r
353 \r
354                 StringFunctions::Convert(index,indexstr);\r
355                 StringFunctions::Convert(identityid,identityidstr);\r
356 \r
357                 message.SetName("ClientGet");\r
358                 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
359                 message["Identifier"]="TrustListRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
360                 message["ReturnType"]="direct";\r
361                 message["MaxSize"]="1000000";                   // 1 MB\r
362 \r
363                 m_fcp->SendMessage(message);\r
364 \r
365                 m_requesting.push_back(identityid);\r
366         }\r
367         st.Finalize();\r
368 \r
369         m_ids[identityid]=true;\r
370 \r
371 }\r