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