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