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
8 #include <Poco/DateTime.h>
\r
9 #include <Poco/Timespan.h>
\r
10 #include <Poco/Timestamp.h>
\r
11 #include <Poco/DateTimeFormatter.h>
\r
17 IntroductionPuzzleRequester::IntroductionPuzzleRequester()
\r
22 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
27 void IntroductionPuzzleRequester::FCPConnected()
\r
29 m_requesting.clear();
\r
33 void IntroductionPuzzleRequester::FCPDisconnected()
\r
38 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
\r
41 SQLite3DB::Statement st;
\r
42 std::vector<std::string> idparts;
\r
44 std::vector<char> data;
\r
45 IntroductionPuzzleXML xml;
\r
48 bool validmessage=true;
\r
50 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
51 StringFunctions::Convert(message["DataLength"],datalength);
\r
52 StringFunctions::Convert(idparts[1],identityid);
\r
53 StringFunctions::Convert(idparts[2],index);
\r
55 // wait for all data to be received from connection
\r
56 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
61 // if we got disconnected- return immediately
\r
62 if(m_fcp->Connected()==false)
\r
68 data.resize(datalength);
\r
71 m_fcp->ReceiveRaw(&data[0],datalength);
\r
74 // parse file into xml and update the database
\r
75 if(data.size()>0 && xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
78 // check if last part of UUID matches first part of public key of identity who inserted it
\r
79 st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
80 st.Bind(0,identityid);
\r
82 if(st.RowReturned())
\r
84 std::vector<std::string> uuidparts;
\r
85 std::vector<std::string> keyparts;
\r
86 std::string keypart="";
\r
87 std::string publickey="";
\r
89 st.ResultText(0,publickey);
\r
91 StringFunctions::SplitMultiple(publickey,"@,",keyparts);
\r
92 StringFunctions::SplitMultiple(xml.GetUUID(),"@",uuidparts);
\r
94 if(uuidparts.size()>1 && keyparts.size()>1)
\r
96 keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");
\r
97 if(keypart!=uuidparts[1])
\r
99 m_log->error("IntroductionPuzzleRequester::HandleAllData UUID in IntroductionPuzzle doesn't match public key of identity : "+message["Identifier"]);
\r
100 validmessage=false;
\r
105 m_log->error("IntroductionPuzzleRequester::HandleAllData Error with identity's public key or UUID : "+message["Identifier"]);
\r
106 validmessage=false;
\r
112 m_log->error("IntroductionPuzzleRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);
\r
113 validmessage=false;
\r
116 // we can only validate bitmaps for now
\r
117 BitmapValidator val;
\r
118 val.SetMax(200,200);
\r
119 std::vector<unsigned char> puzzledata;
\r
120 Base64::Decode(xml.GetPuzzleData(),puzzledata);
\r
121 if(xml.GetMimeType()!="image/bmp" || val.Validate(puzzledata)==false)
\r
123 m_log->error("IntroductionPuzzleRequester::HandleAllData received bad mime type and/or data for "+message["Identifier"]);
\r
124 validmessage=false;
\r
127 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found,UUID,Type,MimeType,PuzzleData) VALUES(?,?,?,?,?,?,?,?);");
\r
128 st.Bind(0,identityid);
\r
129 st.Bind(1,idparts[4]);
\r
134 st.Bind(4,xml.GetUUID());
\r
135 st.Bind(5,xml.GetType());
\r
136 st.Bind(6,xml.GetMimeType());
\r
137 st.Bind(7,xml.GetPuzzleData());
\r
141 st.Bind(3,"false");
\r
150 m_log->debug("IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);
\r
154 // bad data - mark index
\r
155 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
156 st.Bind(0,identityid);
\r
157 st.Bind(1,idparts[4]);
\r
162 m_log->error("IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);
\r
165 // remove this identityid from request list
\r
166 RemoveFromRequestList(identityid);
\r
172 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
\r
174 SQLite3DB::Statement st;
\r
175 std::vector<std::string> idparts;
\r
179 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
180 StringFunctions::Convert(idparts[1],identityid);
\r
181 StringFunctions::Convert(idparts[2],index);
\r
183 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
184 if(message["Fatal"]=="true")
\r
186 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
187 st.Bind(0,identityid);
\r
188 st.Bind(1,idparts[4]);
\r
193 m_log->error("IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
196 // remove this identityid from request list
\r
197 RemoveFromRequestList(identityid);
\r
203 const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)
\r
206 if(message["Identifier"].find("IntroductionPuzzleRequester")==0)
\r
208 if(message.GetName()=="DataFound")
\r
213 if(message.GetName()=="AllData")
\r
215 return HandleAllData(message);
\r
218 if(message.GetName()=="GetFailed")
\r
220 return HandleGetFailed(message);
\r
223 if(message.GetName()=="IdentifierCollision")
\r
225 // remove one of the ids from the requesting list
\r
227 std::vector<std::string> idparts;
\r
228 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
229 StringFunctions::Convert(idparts[1],identityid);
\r
230 RemoveFromRequestList(identityid);
\r
238 void IntroductionPuzzleRequester::Initialize()
\r
241 Option::Instance()->GetInt("MaxIntroductionPuzzleRequests",m_maxrequests);
\r
242 if(m_maxrequests<1)
\r
245 m_log->error("Option MaxIntroductionPuzzleRequests is currently set at less than 1. It must be 1 or greater.");
\r
247 if(m_maxrequests>100)
\r
249 m_log->warning("Option MaxIntroductionPuzzleRequests is currently set at more than 100. This value might be incorrectly configured.");
\r
251 Option::Instance()->Get("MessageBase",m_messagebase);
\r
252 m_tempdate=Poco::Timestamp();
\r
255 void IntroductionPuzzleRequester::PopulateIDList()
\r
257 Poco::DateTime now;
\r
259 std::string limitnum="30";
\r
261 // if we don't have an identity that we haven't seen yet, then set limit to 5
\r
262 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
264 if(!st.RowReturned())
\r
270 // 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
271 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublishTrustList='true' AND PublicKey IS NOT NULL AND PublicKey <> '' AND SingleUse='false' AND (LocalTrustListTrust IS NULL OR LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust')) AND LastSeen>='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' ORDER BY LocalMessageTrust DESC LIMIT 0,"+limitnum+";");
\r
276 while(st.RowReturned())
\r
278 st.ResultInt(0,id);
\r
284 void IntroductionPuzzleRequester::Process()
\r
286 // max is the smaller of the config value or the total number of identities we will request from
\r
287 long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;
\r
289 // try to keep up to max requests going
\r
290 if(m_requesting.size()<max)
\r
292 std::map<long,bool>::iterator i=m_ids.begin();
\r
293 while(i!=m_ids.end() && (*i).second==true)
\r
300 StartRequest((*i).first);
\r
304 // we requested from all ids in the list, repopulate the list
\r
308 // 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
309 // this will recheck for ids every minute
\r
310 Poco::DateTime now;
\r
311 if(m_ids.size()==0 && m_tempdate<(now-Poco::Timespan(0,0,1,0,0)))
\r
318 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
320 thread->RegisterFCPConnected(this);
\r
321 thread->RegisterFCPMessageHandler(this);
\r
322 thread->RegisterPeriodicProcessor(this);
\r
325 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)
\r
327 std::vector<long>::iterator i=m_requesting.begin();
\r
328 while(i!=m_requesting.end() && (*i)!=identityid)
\r
332 if(i!=m_requesting.end())
\r
334 m_requesting.erase(i);
\r
338 void IntroductionPuzzleRequester::StartRequest(const long identityid)
\r
340 Poco::DateTime now;
\r
341 FCPMessage message;
\r
342 std::string publickey;
\r
344 std::string indexstr;
\r
345 std::string identityidstr;
\r
347 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
348 st.Bind(0,identityid);
\r
351 if(st.RowReturned())
\r
353 st.ResultText(0,publickey);
\r
355 now=Poco::Timestamp();
\r
357 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests WHERE Day=? AND IdentityID=?;");
\r
358 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
359 st2.Bind(1,identityid);
\r
363 if(st2.RowReturned())
\r
365 if(st2.ResultNull(0)==false)
\r
367 st2.ResultInt(0,index);
\r
373 StringFunctions::Convert(index,indexstr);
\r
374 StringFunctions::Convert(identityid,identityidstr);
\r
376 message.SetName("ClientGet");
\r
377 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";
\r
378 message["Identifier"]="IntroductionPuzzleRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
379 message["ReturnType"]="direct";
\r
380 message["MaxSize"]="1000000"; // 1 MB
\r
382 m_fcp->SendMessage(message);
\r
384 m_requesting.push_back(identityid);
\r
387 m_ids[identityid]=true;
\r