version 0.2.14
[fms.git] / src / freenet / introductionpuzzlerequester.cpp
1 #include "../../include/freenet/introductionpuzzlerequester.h"\r
2 #include "../../include/freenet/introductionpuzzlexml.h"\r
3 #include "../../include/option.h"\r
4 #include "../../include/stringfunctions.h"\r
5 \r
6 #ifdef XMEM\r
7         #include <xmem.h>\r
8 #endif\r
9 \r
10 IntroductionPuzzleRequester::IntroductionPuzzleRequester()\r
11 {\r
12         Initialize();\r
13 }\r
14 \r
15 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IFCPConnected(fcp)\r
16 {\r
17         Initialize();\r
18 }\r
19 \r
20 void IntroductionPuzzleRequester::FCPConnected()\r
21 {\r
22         m_requesting.clear();\r
23         PopulateIDList();\r
24 }\r
25 \r
26 void IntroductionPuzzleRequester::FCPDisconnected()\r
27 {\r
28         \r
29 }\r
30 \r
31 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)\r
32 {\r
33         DateTime now;\r
34         SQLite3DB::Statement st;\r
35         std::vector<std::string> idparts;\r
36         long datalength;\r
37         std::vector<char> data;\r
38         IntroductionPuzzleXML xml;\r
39         long identityid;\r
40         long index;\r
41         bool validmessage=true;\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                 // check if last part of UUID matches first part of public key of identity who inserted it\r
70                 st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
71                 st.Bind(0,identityid);\r
72                 st.Step();\r
73                 if(st.RowReturned())\r
74                 {\r
75                         std::vector<std::string> uuidparts;\r
76                         std::vector<std::string> keyparts;\r
77                         std::string keypart="";\r
78                         std::string publickey="";\r
79 \r
80                         st.ResultText(0,publickey);\r
81 \r
82                         StringFunctions::SplitMultiple(publickey,"@,",keyparts);\r
83                         StringFunctions::SplitMultiple(xml.GetUUID(),"@",uuidparts);\r
84 \r
85                         if(uuidparts.size()>1 && keyparts.size()>1)\r
86                         {\r
87                                 keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");\r
88                                 if(keypart!=uuidparts[1])\r
89                                 {\r
90                                         m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData UUID in IntroductionPuzzle doesn't match public key of identity : "+message["Identifier"]);\r
91                                         validmessage=false;\r
92                                 }\r
93                         }\r
94                         else\r
95                         {\r
96                                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData Error with identity's public key or UUID : "+message["Identifier"]);\r
97                                 validmessage=false;\r
98                         }\r
99 \r
100                 }\r
101                 else\r
102                 {\r
103                         m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);\r
104                         validmessage=false;\r
105                 }\r
106 \r
107                 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found,UUID,Type,MimeType,PuzzleData) VALUES(?,?,?,?,?,?,?,?);");\r
108                 st.Bind(0,identityid);\r
109                 st.Bind(1,idparts[4]);\r
110                 st.Bind(2,index);\r
111                 if(validmessage)\r
112                 {\r
113                         st.Bind(3,"true");\r
114                         st.Bind(4,xml.GetUUID());\r
115                         st.Bind(5,xml.GetType());\r
116                         st.Bind(6,xml.GetMimeType());\r
117                         st.Bind(7,xml.GetPuzzleData());\r
118                 }\r
119                 else\r
120                 {\r
121                         st.Bind(3,"false");\r
122                         st.Bind(4);\r
123                         st.Bind(5);\r
124                         st.Bind(6);\r
125                         st.Bind(7);\r
126                 }\r
127                 st.Step();\r
128                 st.Finalize();\r
129 \r
130                 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);\r
131         }\r
132         else\r
133         {\r
134                 // bad data - mark index\r
135                 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
136                 st.Bind(0,identityid);\r
137                 st.Bind(1,idparts[4]);\r
138                 st.Bind(2,index);\r
139                 st.Step();\r
140                 st.Finalize();\r
141 \r
142                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);\r
143         }\r
144 \r
145         // remove this identityid from request list\r
146         RemoveFromRequestList(identityid);\r
147 \r
148         return true;\r
149 \r
150 }\r
151 \r
152 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)\r
153 {\r
154         DateTime now;\r
155         SQLite3DB::Statement st;\r
156         std::vector<std::string> idparts;\r
157         long identityid;\r
158         long index;\r
159 \r
160         now.SetToGMTime();\r
161         StringFunctions::Split(message["Identifier"],"|",idparts);\r
162         StringFunctions::Convert(idparts[1],identityid);\r
163         StringFunctions::Convert(idparts[2],index);     \r
164 \r
165         // if this is a fatal error - insert index into database so we won't try to download this index again\r
166         if(message["Fatal"]=="true")\r
167         {\r
168                 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
169                 st.Bind(0,identityid);\r
170                 st.Bind(1,idparts[4]);\r
171                 st.Bind(2,index);\r
172                 st.Step();\r
173                 st.Finalize();\r
174 \r
175                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
176         }\r
177 \r
178         // remove this identityid from request list\r
179         RemoveFromRequestList(identityid);\r
180 \r
181         return true;\r
182 \r
183 }\r
184 \r
185 const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)\r
186 {\r
187 \r
188         if(message["Identifier"].find("IntroductionPuzzleRequester")==0)\r
189         {\r
190                 if(message.GetName()=="DataFound")\r
191                 {\r
192                         return true;\r
193                 }\r
194 \r
195                 if(message.GetName()=="AllData")\r
196                 {\r
197                         return HandleAllData(message);\r
198                 }\r
199 \r
200                 if(message.GetName()=="GetFailed")\r
201                 {\r
202                         return HandleGetFailed(message);\r
203                 }\r
204                 \r
205                 if(message.GetName()=="IdentifierCollision")\r
206                 {\r
207                         // remove one of the ids from the requesting list\r
208                         long identityid=0;\r
209                         std::vector<std::string> idparts;\r
210                         StringFunctions::Split(message["Identifier"],"|",idparts);\r
211                         StringFunctions::Convert(idparts[1],identityid);\r
212                         RemoveFromRequestList(identityid);\r
213                         return true;\r
214                 }\r
215         }\r
216 \r
217         return false;\r
218 }\r
219 \r
220 void IntroductionPuzzleRequester::Initialize()\r
221 {\r
222         std::string tempval="";\r
223         Option::Instance()->Get("MaxIntroductionPuzzleRequests",tempval);\r
224         StringFunctions::Convert(tempval,m_maxrequests);\r
225         if(m_maxrequests<1)\r
226         {\r
227                 m_maxrequests=1;\r
228                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+".  It must be 1 or greater.");\r
229         }\r
230         if(m_maxrequests>100)\r
231         {\r
232                 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
233         }\r
234         Option::Instance()->Get("MessageBase",m_messagebase);\r
235         m_tempdate.SetToGMTime();\r
236 }\r
237 \r
238 void IntroductionPuzzleRequester::PopulateIDList()\r
239 {\r
240         DateTime now;\r
241         int id;\r
242         std::string limitnum="30";\r
243 \r
244         // if we don't have an identity that we haven't seen yet, then set limit to 5\r
245         SQLite3DB::Statement st=m_db->Prepare("SELECT tblLocalIdentity.LocalIdentityID FROM tblLocalIdentity LEFT JOIN tblIdentity ON tblLocalIdentity.PublicKey=tblIdentity.PublicKey WHERE tblIdentity.IdentityID IS NULL;");\r
246         st.Step();\r
247         if(!st.RowReturned())\r
248         {\r
249                 limitnum="5";\r
250         }\r
251         st.Finalize();\r
252 \r
253         now.SetToGMTime();\r
254 \r
255         // select identities that aren't single use, are publishing a trust list, and have been seen today ( order by trust DESC and limit to limitnum )\r
256         st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublishTrustList='true' AND PublicKey IS NOT NULL AND PublicKey <> '' AND SingleUse='false' AND LastSeen>='"+now.Format("%Y-%m-%d")+"' ORDER BY LocalMessageTrust DESC LIMIT 0,"+limitnum+";");\r
257         st.Step();\r
258 \r
259         m_ids.clear();\r
260 \r
261         while(st.RowReturned())\r
262         {\r
263                 st.ResultInt(0,id);\r
264                 m_ids[id]=false;\r
265                 st.Step();\r
266         }\r
267 }\r
268 \r
269 void IntroductionPuzzleRequester::Process()\r
270 {\r
271         // max is the smaller of the config value or the total number of identities we will request from\r
272         long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;\r
273 \r
274         // try to keep up to max requests going\r
275         if(m_requesting.size()<max)\r
276         {\r
277                 std::map<long,bool>::iterator i=m_ids.begin();\r
278                 while(i!=m_ids.end() && (*i).second==true)\r
279                 {\r
280                         i++;\r
281                 }\r
282 \r
283                 if(i!=m_ids.end())\r
284                 {\r
285                         StartRequest((*i).first);\r
286                 }\r
287                 else\r
288                 {\r
289                         // we requested from all ids in the list, repopulate the list\r
290                         PopulateIDList();\r
291                 }\r
292         }\r
293         // 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
294         // this will recheck for ids every minute\r
295         DateTime now;\r
296         now.SetToGMTime();\r
297         if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))\r
298         {\r
299                 PopulateIDList();\r
300                 m_tempdate=now;\r
301         }\r
302 }\r
303 \r
304 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)\r
305 {\r
306         thread->RegisterFCPConnected(this);\r
307         thread->RegisterFCPMessageHandler(this);\r
308         thread->RegisterPeriodicProcessor(this);\r
309 }\r
310 \r
311 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)\r
312 {\r
313         std::vector<long>::iterator i=m_requesting.begin();\r
314         while(i!=m_requesting.end() && (*i)!=identityid)\r
315         {\r
316                 i++;\r
317         }\r
318         if(i!=m_requesting.end())\r
319         {\r
320                 m_requesting.erase(i);\r
321         }\r
322 }\r
323 \r
324 void IntroductionPuzzleRequester::StartRequest(const long identityid)\r
325 {\r
326         DateTime now;\r
327         FCPMessage message;\r
328         std::string publickey;\r
329         int index;\r
330         std::string indexstr;\r
331         std::string identityidstr;\r
332 \r
333         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
334         st.Bind(0,identityid);\r
335         st.Step();\r
336 \r
337         if(st.RowReturned())\r
338         {\r
339                 st.ResultText(0,publickey);\r
340 \r
341                 now.SetToGMTime();\r
342 \r
343                 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests WHERE Day=? AND IdentityID=?;");\r
344                 st2.Bind(0,now.Format("%Y-%m-%d"));\r
345                 st2.Bind(1,identityid);\r
346                 st2.Step();\r
347 \r
348                 index=0;\r
349                 if(st2.RowReturned())\r
350                 {\r
351                         if(st2.ResultNull(0)==false)\r
352                         {\r
353                                 st2.ResultInt(0,index);\r
354                                 index++;\r
355                         }\r
356                 }\r
357                 st2.Finalize();\r
358 \r
359                 StringFunctions::Convert(index,indexstr);\r
360                 StringFunctions::Convert(identityid,identityidstr);\r
361 \r
362                 message.SetName("ClientGet");\r
363                 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";\r
364                 message["Identifier"]="IntroductionPuzzleRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
365                 message["ReturnType"]="direct";\r
366                 message["MaxSize"]="1000000";           // 1 MB\r
367 \r
368                 m_fcp->SendMessage(message);\r
369                 \r
370                 m_requesting.push_back(identityid);\r
371         }\r
372 \r
373         m_ids[identityid]=true;\r
374 }\r