version 0.3.0
[fms.git] / src / freenet / trustlistrequester.cpp
1 #include "../../include/freenet/trustlistrequester.h"\r
2 #include "../../include/option.h"\r
3 #include "../../include/stringfunctions.h"\r
4 #include "../../include/freenet/trustlistxml.h"\r
5 \r
6 #include <Poco/DateTimeFormatter.h>\r
7 \r
8 #ifdef XMEM\r
9         #include <xmem.h>\r
10 #endif\r
11 \r
12 TrustListRequester::TrustListRequester()\r
13 {\r
14         Initialize();\r
15 }\r
16 \r
17 TrustListRequester::TrustListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)\r
18 {\r
19         Initialize();\r
20 }\r
21 \r
22 const bool TrustListRequester::HandleAllData(FCPMessage &message)\r
23 {\r
24         Poco::DateTime now;\r
25         SQLite3DB::Statement st;\r
26         SQLite3DB::Statement trustst;\r
27         std::vector<std::string> idparts;\r
28         long datalength;\r
29         std::vector<char> data;\r
30         TrustListXML xml;\r
31         long identityid;\r
32         long index;\r
33 \r
34         StringFunctions::Split(message["Identifier"],"|",idparts);\r
35         StringFunctions::Convert(message["DataLength"],datalength);\r
36         StringFunctions::Convert(idparts[1],identityid);\r
37         StringFunctions::Convert(idparts[2],index);\r
38 \r
39         // wait for all data to be received from connection\r
40         while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
41         {\r
42                 m_fcp->Update(1);\r
43         }\r
44 \r
45         // if we got disconnected- return immediately\r
46         if(m_fcp->Connected()==false)\r
47         {\r
48                 return false;\r
49         }\r
50 \r
51         // receive the file\r
52         data.resize(datalength);\r
53         m_fcp->ReceiveRaw(&data[0],datalength);\r
54 \r
55         // parse file into xml and update the database\r
56         if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
57         {\r
58                 // find the identity name and public key of the identity publishing the trust list\r
59                 std::string publisherid="";\r
60                 st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
61                 st.Bind(0,identityid);\r
62                 st.Step();\r
63                 if(st.RowReturned())\r
64                 {\r
65                         std::string publishername="";\r
66                         std::string publisherpublickey="";\r
67                         st.ResultText(0,publishername);\r
68                         st.ResultText(1,publisherpublickey);\r
69                         publisherid=publishername;\r
70                         if(publisherpublickey.size()>4)\r
71                         {\r
72                                 publisherid+=publisherpublickey.substr(3,44);\r
73                         }\r
74                 }\r
75                 st.Finalize();\r
76 \r
77                 // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file\r
78                 st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");\r
79                 st.Bind(0,identityid);\r
80                 st.Step();\r
81                 st.Finalize();\r
82 \r
83                 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");\r
84                 trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust,MessageTrustComment,TrustListTrustComment) VALUES(?,?,?,?,?,?);");\r
85                 \r
86                 SQLite3DB::Statement idinsert=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES(?,?,?);");\r
87                 \r
88                 // loop through all trust entries in xml and add to database if we don't already know them\r
89                 for(long i=0; i<xml.TrustCount(); i++)\r
90                 {\r
91                         int id;\r
92                         std::string identity;\r
93                         std::string messagetrustcomment="";\r
94                         std::string trustlisttrustcomment="";\r
95                         identity=xml.GetIdentity(i);\r
96 \r
97                         st.Bind(0,identity);\r
98                         st.Step();\r
99                         if(st.RowReturned()==false)\r
100                         {\r
101                                 idinsert.Bind(0,identity);\r
102                                 idinsert.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
103                                 idinsert.Bind(2,"trust list of "+publisherid);\r
104                                 idinsert.Step(true);\r
105                                 id=idinsert.GetLastInsertRowID();\r
106                                 idinsert.Reset();\r
107                         //      m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);\r
108                         }\r
109                         else\r
110                         {\r
111                                 st.ResultInt(0,id);\r
112                         }\r
113                         st.Reset();\r
114 \r
115                         //insert trust for this identity\r
116                         trustst.Bind(0,identityid);\r
117                         trustst.Bind(1,id);\r
118                         if(xml.GetMessageTrust(i)==-1)\r
119                         {\r
120                                 trustst.Bind(2);\r
121                         }\r
122                         else\r
123                         {\r
124                                 trustst.Bind(2,xml.GetMessageTrust(i));\r
125                         }\r
126                         if(xml.GetTrustListTrust(i)==-1)\r
127                         {\r
128                                 trustst.Bind(3);\r
129                         }\r
130                         else\r
131                         {\r
132                                 trustst.Bind(3,xml.GetTrustListTrust(i));\r
133                         }\r
134                         messagetrustcomment=xml.GetMessageTrustComment(i);\r
135                         trustlisttrustcomment=xml.GetTrustListTrustComment(i);\r
136                         // limit comments to 50 characters each\r
137                         if(messagetrustcomment.size()>50)\r
138                         {\r
139                                 messagetrustcomment.erase(50);\r
140                         }\r
141                         if(trustlisttrustcomment.size()>50)\r
142                         {\r
143                                 trustlisttrustcomment.erase(50);\r
144                         }\r
145                         trustst.Bind(4,messagetrustcomment);\r
146                         trustst.Bind(5,trustlisttrustcomment);\r
147                         trustst.Step();\r
148                         trustst.Reset();\r
149 \r
150                 }\r
151                 trustst.Finalize();\r
152                 st.Finalize();\r
153 \r
154                 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");\r
155                 st.Bind(0,identityid);\r
156                 st.Bind(1,idparts[4]);\r
157                 st.Bind(2,index);\r
158                 st.Step();\r
159                 st.Finalize();\r
160 \r
161                 m_log->debug("TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);\r
162         }\r
163         else\r
164         {\r
165                 // bad data - mark index\r
166                 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
167                 st.Bind(0,identityid);\r
168                 st.Bind(1,idparts[4]);\r
169                 st.Bind(2,index);\r
170                 st.Step();\r
171                 st.Finalize();\r
172 \r
173                 m_log->error("TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);\r
174         }\r
175 \r
176         // remove this identityid from request list\r
177         RemoveFromRequestList(identityid);\r
178 \r
179         return true;\r
180 \r
181 }\r
182 \r
183 const bool TrustListRequester::HandleGetFailed(FCPMessage &message)\r
184 {\r
185         SQLite3DB::Statement st;\r
186         std::vector<std::string> idparts;\r
187         long identityid;\r
188         long index;\r
189 \r
190         StringFunctions::Split(message["Identifier"],"|",idparts);\r
191         StringFunctions::Convert(idparts[1],identityid);\r
192         StringFunctions::Convert(idparts[2],index);     \r
193 \r
194         // if this is a fatal error - insert index into database so we won't try to download this index again\r
195         if(message["Fatal"]=="true")\r
196         {\r
197                 st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
198                 st.Bind(0,identityid);\r
199                 st.Bind(1,idparts[4]);\r
200                 st.Bind(2,index);\r
201                 st.Step();\r
202                 st.Finalize();\r
203 \r
204                 m_log->error("TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
205         }\r
206 \r
207         // remove this identityid from request list\r
208         RemoveFromRequestList(identityid);\r
209 \r
210         return true;\r
211 \r
212 }\r
213 \r
214 void TrustListRequester::Initialize()\r
215 {\r
216         std::string tempval="";\r
217         m_fcpuniquename="TrustListRequester";\r
218         Option::Instance()->Get("MaxIdentityRequests",tempval);\r
219         StringFunctions::Convert(tempval,m_maxrequests);\r
220         if(m_maxrequests<1)\r
221         {\r
222                 m_maxrequests=1;\r
223                 m_log->error("Option MaxTrustListRequests is currently set at "+tempval+".  It must be 1 or greater.");\r
224         }\r
225         if(m_maxrequests>100)\r
226         {\r
227                 m_log->warning("Option MaxTrustListRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
228         }\r
229         m_tempdate=Poco::Timestamp();\r
230 }\r
231 \r
232 void TrustListRequester::PopulateIDList()\r
233 {\r
234         Poco::DateTime date;\r
235         int id;\r
236         std::string sql;\r
237 \r
238         // select identities we want to query (we've seen them today and they are publishing trust list) - sort by their trust level (descending) with secondary sort on how long ago we saw them (ascending)\r
239         sql="SELECT IdentityID FROM tblIdentity ";\r
240         sql+="WHERE Name IS NOT NULL AND Name <> '' AND PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"' AND PublishTrustList='true' AND LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust') AND ( PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust') )";\r
241         sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";\r
242 \r
243         SQLite3DB::Statement st=m_db->Prepare(sql);\r
244         st.Step();\r
245 \r
246         m_ids.clear();\r
247 \r
248         while(st.RowReturned())\r
249         {\r
250                 st.ResultInt(0,id);\r
251                 m_ids[id]=false;\r
252                 st.Step();\r
253         }\r
254 }\r
255 \r
256 void TrustListRequester::StartRequest(const long &identityid)\r
257 {\r
258         Poco::DateTime now;\r
259         FCPMessage message;\r
260         std::string publickey;\r
261         int index;\r
262         std::string indexstr;\r
263         std::string identityidstr;\r
264 \r
265         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
266         st.Bind(0,identityid);\r
267         st.Step();\r
268 \r
269         if(st.RowReturned())\r
270         {\r
271                 st.ResultText(0,publickey);\r
272 \r
273                 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");\r
274                 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));\r
275                 st2.Bind(1,identityid);\r
276                 st2.Step();\r
277 \r
278                 index=0;\r
279                 if(st2.RowReturned())\r
280                 {\r
281                         if(st2.ResultNull(0)==false)\r
282                         {\r
283                                 st2.ResultInt(0,index);\r
284                                 index++;\r
285                         }\r
286                 }\r
287                 st2.Finalize();\r
288 \r
289                 StringFunctions::Convert(index,indexstr);\r
290                 StringFunctions::Convert(identityid,identityidstr);\r
291 \r
292                 message.SetName("ClientGet");\r
293                 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
294                 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
295                 message["ReturnType"]="direct";\r
296                 message["MaxSize"]="1000000";                   // 1 MB\r
297 \r
298                 m_fcp->SendMessage(message);\r
299 \r
300                 m_requesting.push_back(identityid);\r
301         }\r
302         st.Finalize();\r
303 \r
304         m_ids[identityid]=true;\r
305 \r
306 }\r