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
12 IntroductionPuzzleRequester::IntroductionPuzzleRequester()
\r
17 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
22 void IntroductionPuzzleRequester::FCPConnected()
\r
24 m_requesting.clear();
\r
28 void IntroductionPuzzleRequester::FCPDisconnected()
\r
33 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
\r
36 SQLite3DB::Statement st;
\r
37 std::vector<std::string> idparts;
\r
39 std::vector<char> data;
\r
40 IntroductionPuzzleXML xml;
\r
43 bool validmessage=true;
\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
51 // wait for all data to be received from connection
\r
52 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
57 // if we got disconnected- return immediately
\r
58 if(m_fcp->Connected()==false)
\r
64 data.resize(datalength);
\r
65 m_fcp->ReceiveRaw(&data[0],datalength);
\r
67 // parse file into xml and update the database
\r
68 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\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
75 if(st.RowReturned())
\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
82 st.ResultText(0,publickey);
\r
84 StringFunctions::SplitMultiple(publickey,"@,",keyparts);
\r
85 StringFunctions::SplitMultiple(xml.GetUUID(),"@",uuidparts);
\r
87 if(uuidparts.size()>1 && keyparts.size()>1)
\r
89 keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");
\r
90 if(keypart!=uuidparts[1])
\r
92 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData UUID in IntroductionPuzzle doesn't match public key of identity : "+message["Identifier"]);
\r
98 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData Error with identity's public key or UUID : "+message["Identifier"]);
\r
105 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);
\r
106 validmessage=false;
\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
115 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData received bad mime type and/or data for "+message["Identifier"]);
\r
116 validmessage=false;
\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
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
133 st.Bind(3,"false");
\r
142 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);
\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
154 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);
\r
157 // remove this identityid from request list
\r
158 RemoveFromRequestList(identityid);
\r
164 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
\r
167 SQLite3DB::Statement st;
\r
168 std::vector<std::string> idparts;
\r
173 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
174 StringFunctions::Convert(idparts[1],identityid);
\r
175 StringFunctions::Convert(idparts[2],index);
\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
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
187 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
190 // remove this identityid from request list
\r
191 RemoveFromRequestList(identityid);
\r
197 const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)
\r
200 if(message["Identifier"].find("IntroductionPuzzleRequester")==0)
\r
202 if(message.GetName()=="DataFound")
\r
207 if(message.GetName()=="AllData")
\r
209 return HandleAllData(message);
\r
212 if(message.GetName()=="GetFailed")
\r
214 return HandleGetFailed(message);
\r
217 if(message.GetName()=="IdentifierCollision")
\r
219 // remove one of the ids from the requesting list
\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
232 void IntroductionPuzzleRequester::Initialize()
\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
240 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
242 if(m_maxrequests>100)
\r
244 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
246 Option::Instance()->Get("MessageBase",m_messagebase);
\r
247 m_tempdate.SetToGMTime();
\r
250 void IntroductionPuzzleRequester::PopulateIDList()
\r
254 std::string limitnum="30";
\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
259 if(!st.RowReturned())
\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
273 while(st.RowReturned())
\r
275 st.ResultInt(0,id);
\r
281 void IntroductionPuzzleRequester::Process()
\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
286 // try to keep up to max requests going
\r
287 if(m_requesting.size()<max)
\r
289 std::map<long,bool>::iterator i=m_ids.begin();
\r
290 while(i!=m_ids.end() && (*i).second==true)
\r
297 StartRequest((*i).first);
\r
301 // we requested from all ids in the list, repopulate the list
\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
309 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
316 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
318 thread->RegisterFCPConnected(this);
\r
319 thread->RegisterFCPMessageHandler(this);
\r
320 thread->RegisterPeriodicProcessor(this);
\r
323 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)
\r
325 std::vector<long>::iterator i=m_requesting.begin();
\r
326 while(i!=m_requesting.end() && (*i)!=identityid)
\r
330 if(i!=m_requesting.end())
\r
332 m_requesting.erase(i);
\r
336 void IntroductionPuzzleRequester::StartRequest(const long identityid)
\r
339 FCPMessage message;
\r
340 std::string publickey;
\r
342 std::string indexstr;
\r
343 std::string identityidstr;
\r
345 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
346 st.Bind(0,identityid);
\r
349 if(st.RowReturned())
\r
351 st.ResultText(0,publickey);
\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
361 if(st2.RowReturned())
\r
363 if(st2.ResultNull(0)==false)
\r
365 st2.ResultInt(0,index);
\r
371 StringFunctions::Convert(index,indexstr);
\r
372 StringFunctions::Convert(identityid,identityidstr);
\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
380 m_fcp->SendMessage(message);
\r
382 m_requesting.push_back(identityid);
\r
385 m_ids[identityid]=true;
\r