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
10 IntroductionPuzzleRequester::IntroductionPuzzleRequester()
\r
15 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IFCPConnected(fcp)
\r
20 void IntroductionPuzzleRequester::FCPConnected()
\r
22 m_requesting.clear();
\r
26 void IntroductionPuzzleRequester::FCPDisconnected()
\r
31 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
\r
34 SQLite3DB::Statement st;
\r
35 std::vector<std::string> idparts;
\r
37 std::vector<char> data;
\r
38 IntroductionPuzzleXML xml;
\r
41 bool validmessage=true;
\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
49 // wait for all data to be received from connection
\r
50 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
55 // if we got disconnected- return immediately
\r
56 if(m_fcp->Connected()==false)
\r
62 data.resize(datalength);
\r
63 m_fcp->ReceiveRaw(&data[0],datalength);
\r
65 // parse file into xml and update the database
\r
66 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\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
73 if(st.RowReturned())
\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
80 st.ResultText(0,publickey);
\r
82 StringFunctions::SplitMultiple(publickey,"@,",keyparts);
\r
83 StringFunctions::SplitMultiple(xml.GetUUID(),"@",uuidparts);
\r
85 if(uuidparts.size()>1 && keyparts.size()>1)
\r
87 keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");
\r
88 if(keypart!=uuidparts[1])
\r
90 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData UUID in IntroductionPuzzle doesn't match public key of identity : "+message["Identifier"]);
\r
96 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData Error with identity's public key or UUID : "+message["Identifier"]);
\r
103 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);
\r
104 validmessage=false;
\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
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
121 st.Bind(3,"false");
\r
130 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);
\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
142 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);
\r
145 // remove this identityid from request list
\r
146 RemoveFromRequestList(identityid);
\r
152 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
\r
155 SQLite3DB::Statement st;
\r
156 std::vector<std::string> idparts;
\r
161 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
162 StringFunctions::Convert(idparts[1],identityid);
\r
163 StringFunctions::Convert(idparts[2],index);
\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
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
175 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
178 // remove this identityid from request list
\r
179 RemoveFromRequestList(identityid);
\r
185 const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)
\r
188 if(message["Identifier"].find("IntroductionPuzzleRequester")==0)
\r
190 if(message.GetName()=="DataFound")
\r
195 if(message.GetName()=="AllData")
\r
197 return HandleAllData(message);
\r
200 if(message.GetName()=="GetFailed")
\r
202 return HandleGetFailed(message);
\r
205 if(message.GetName()=="IdentifierCollision")
\r
207 // remove one of the ids from the requesting list
\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
220 void IntroductionPuzzleRequester::Initialize()
\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
228 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
230 if(m_maxrequests>100)
\r
232 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
234 Option::Instance()->Get("MessageBase",m_messagebase);
\r
235 m_tempdate.SetToGMTime();
\r
238 void IntroductionPuzzleRequester::PopulateIDList()
\r
242 std::string limitnum="30";
\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
247 if(!st.RowReturned())
\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
261 while(st.RowReturned())
\r
263 st.ResultInt(0,id);
\r
269 void IntroductionPuzzleRequester::Process()
\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
274 // try to keep up to max requests going
\r
275 if(m_requesting.size()<max)
\r
277 std::map<long,bool>::iterator i=m_ids.begin();
\r
278 while(i!=m_ids.end() && (*i).second==true)
\r
285 StartRequest((*i).first);
\r
289 // we requested from all ids in the list, repopulate the list
\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
297 if(m_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))
\r
304 void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)
\r
306 thread->RegisterFCPConnected(this);
\r
307 thread->RegisterFCPMessageHandler(this);
\r
308 thread->RegisterPeriodicProcessor(this);
\r
311 void IntroductionPuzzleRequester::RemoveFromRequestList(const long identityid)
\r
313 std::vector<long>::iterator i=m_requesting.begin();
\r
314 while(i!=m_requesting.end() && (*i)!=identityid)
\r
318 if(i!=m_requesting.end())
\r
320 m_requesting.erase(i);
\r
324 void IntroductionPuzzleRequester::StartRequest(const long identityid)
\r
327 FCPMessage message;
\r
328 std::string publickey;
\r
330 std::string indexstr;
\r
331 std::string identityidstr;
\r
333 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
334 st.Bind(0,identityid);
\r
337 if(st.RowReturned())
\r
339 st.ResultText(0,publickey);
\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
349 if(st2.RowReturned())
\r
351 if(st2.ResultNull(0)==false)
\r
353 st2.ResultInt(0,index);
\r
359 StringFunctions::Convert(index,indexstr);
\r
360 StringFunctions::Convert(identityid,identityidstr);
\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
368 m_fcp->SendMessage(message);
\r
370 m_requesting.push_back(identityid);
\r
373 m_ids[identityid]=true;
\r