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