fixes
[oweals/gnunet.git] / src / dht / plugin_dhtlog_mysql_dump.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006 - 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/dht/plugin_dhtlog_mysql_dump.c
23  * @brief MySQL logging plugin to record DHT operations to MySQL server,
24  *        but write all queries to file instead of the actual server
25  *        so that they can be imported later.  The idea is that connecting
26  *        to the MySQL server X times can be really problematic, but hopefully
27  *        writing to a single file is more reliable.
28  * @author Nathan Evans
29  *
30  * Database: MySQL
31  */
32
33 #include "platform.h"
34 #include "gnunet_util_lib.h"
35 #include "dhtlog.h"
36
37
38 #define DEBUG_DHTLOG GNUNET_NO
39
40 /**
41  * Maximum number of supported parameters for a prepared
42  * statement.  Increase if needed.
43  */
44 #define MAX_PARAM 32
45
46
47 static unsigned long max_varchar_len;
48
49 /**
50  * The configuration the DHT service is running with
51  */
52 static const struct GNUNET_CONFIGURATION_Handle *cfg;
53
54 #define INSERT_QUERIES_STMT "prepare insert_query from 'INSERT INTO queries (trialuid, querytype, hops, dhtkeyuid, dhtqueryid, succeeded, nodeuid) "\
55                           "VALUES (@temp_trial, ?, ?, ?, ?, ?, ?)'"
56
57 #define INSERT_ROUTES_STMT "prepare insert_route from 'INSERT INTO routes (trialuid, querytype, hops, dhtkeyuid, dhtqueryid, succeeded, nodeuid, from_node, to_node) "\
58                           "VALUES (@temp_trial, ?, ?, ?, ?, ?, ?, ?, ?)'"
59
60 #define INSERT_NODES_STMT "prepare insert_node from 'INSERT ignore INTO nodes (trialuid, nodeid) "\
61                           "VALUES (@temp_trial, ?)'"
62
63 #define INSERT_TOPOLOGY_STMT "prepare insert_topology from 'INSERT INTO topology (trialuid, date, connections) "\
64                              "VALUES (@temp_trial, ?, ?)'"
65
66 #define EXTEND_TOPOLOGY_STMT "prepare extend_topology from 'INSERT INTO extended_topology (topology_uid, uid_first, uid_second) "\
67                              "VALUES (@temp_topology, ?, ?)'"
68
69 #define UPDATE_TOPOLOGY_STMT "prepare update_topology from 'update topology set connections = ?  where topology_uid = @temp_topology'"
70
71 #define SET_MALICIOUS_STMT "prepare set_malicious from 'update nodes set malicious_dropper = 1  where trialuid = @temp_trial and nodeid = @temp_node'"
72
73 #define INSERT_TRIALS_STMT "prepare insert_trial from 'INSERT INTO trials"\
74                            "(starttime, numnodes, topology,"\
75                            "topology_percentage, topology_probability,"\
76                            "blacklist_topology, connect_topology, connect_topology_option,"\
77                            "connect_topology_option_modifier, puts, gets, "\
78                            "concurrent, settle_time, num_rounds, malicious_getters,"\
79                            "malicious_putters, malicious_droppers, malicious_get_frequency,"\
80                            "malicious_put_frequency, stop_closest, stop_found, strict_kademlia, "\
81                            "gets_succeeded, message) "\
82                            "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'"
83
84 #define INSERT_GENERIC_STAT_STMT "prepare insert_generic_stat from 'INSERT INTO generic_stats" \
85                                  "(trialuid, nodeuid, section, name, value)"\
86                                  "VALUES (@temp_trial, @temp_node, @temp_section, @temp_stat, @temp_value)'"
87
88 #define INSERT_STAT_STMT "prepare insert_stat from 'INSERT INTO node_statistics"\
89                             "(trialuid, nodeuid, route_requests,"\
90                             "route_forwards, result_requests,"\
91                             "client_results, result_forwards, gets,"\
92                             "puts, data_inserts, find_peer_requests, "\
93                             "find_peers_started, gets_started, puts_started, find_peer_responses_received,"\
94                             "get_responses_received, find_peer_responses_sent, get_responses_sent) "\
95                             "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'"
96
97 #define INSERT_DHTKEY_STMT "prepare insert_dhtkey from 'INSERT ignore INTO dhtkeys (dhtkey, trialuid) "\
98                            "VALUES (?, @temp_trial)'"
99
100 #define UPDATE_TRIALS_STMT "prepare update_trial from 'UPDATE trials set endtime= ?, gets_succeeded = ? where trialuid = @temp_trial'"
101
102 #define UPDATE_CONNECTIONS_STMT "prepare update_conn from 'UPDATE trials set totalConnections = ? where trialuid = @temp_trial'"
103
104 #define GET_TRIAL_STMT "prepare select_trial from 'SELECT MAX( trialuid ) FROM trials into @temp_trial'"
105
106 #define GET_TOPOLOGY_STMT "prepare select_topology from 'SELECT MAX( topology_uid ) FROM topology into @temp_topology'"
107
108 #define GET_DHTKEYUID_STMT "prepare get_dhtkeyuid from 'SELECT dhtkeyuid FROM dhtkeys where dhtkey = ? and trialuid = @temp_trial'"
109
110 #define GET_NODEUID_STMT "prepare get_nodeuid from 'SELECT nodeuid FROM nodes where trialuid = @temp_trial and nodeid = ?'"
111
112 #define DATE_STR_SIZE 50
113
114 /**
115  * File to dump all sql statements to.
116  */
117 FILE *outfile;
118
119
120 static char *
121 get_sql_time()
122 {
123   static char date[DATE_STR_SIZE];
124   time_t timetmp;
125   struct tm *tmptr;
126
127   time (&timetmp);
128   memset (date, 0, DATE_STR_SIZE);
129   tmptr = localtime (&timetmp);
130   if (NULL != tmptr)
131     strftime (date, DATE_STR_SIZE, "%Y-%m-%d %H:%M:%S", tmptr);
132   else
133     strcpy (date, "");
134
135   return date;
136 }
137
138 /**
139  * Create a prepared statement.
140  *
141  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
142  */
143 static int
144 prepared_statement_create (const char *statement)
145 {
146   if (fprintf(outfile, "%s;\n", statement) > 0)
147     return GNUNET_OK;
148
149   return GNUNET_SYSERR;
150 }
151
152 /*
153  * Initialize the prepared statements for use with dht test logging
154  */
155 static int
156 iopen ()
157 {
158 #define PINIT(a) (GNUNET_OK != (prepared_statement_create(a)))
159   if (PINIT (INSERT_QUERIES_STMT) ||
160       PINIT (INSERT_ROUTES_STMT) ||
161       PINIT (INSERT_TRIALS_STMT) ||
162       PINIT (SET_MALICIOUS_STMT) ||
163       PINIT (INSERT_GENERIC_STAT_STMT) ||
164       PINIT (INSERT_STAT_STMT) ||
165       PINIT (INSERT_NODES_STMT) ||
166       PINIT (INSERT_DHTKEY_STMT) ||
167       PINIT (UPDATE_TRIALS_STMT) ||
168       PINIT (GET_DHTKEYUID_STMT) ||
169       PINIT (GET_NODEUID_STMT) ||
170       PINIT (UPDATE_CONNECTIONS_STMT) ||
171       PINIT (INSERT_TOPOLOGY_STMT) ||
172       PINIT (EXTEND_TOPOLOGY_STMT) ||
173       PINIT (UPDATE_TOPOLOGY_STMT) ||
174       PINIT (GET_TRIAL_STMT) ||
175       PINIT (GET_TOPOLOGY_STMT))
176     {
177       return GNUNET_SYSERR;
178     }
179 #undef PINIT
180
181   return GNUNET_OK;
182 }
183
184
185
186 /*
187  * Records the current topology (number of connections, time, trial)
188  *
189  * @param num_connections how many connections are in the topology
190  *
191  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
192  */
193 int
194 add_topology (int num_connections)
195 {
196   int ret;
197   if (outfile == NULL)
198     return GNUNET_SYSERR;
199
200   ret = fprintf(outfile, "set @date = \"%s\", @num = %d;\n", get_sql_time(), num_connections);
201
202   if (ret < 0)
203     return GNUNET_SYSERR;
204   ret = fprintf(outfile, "execute insert_topology using "
205                          "@date, @num;\n");
206
207   ret = fprintf(outfile, "execute select_topology;\n");
208
209   if (ret >= 0)
210     return GNUNET_OK;
211   return GNUNET_SYSERR;
212 }
213
214 /*
215  * Records a connection between two peers in the current topology
216  *
217  * @param first one side of the connection
218  * @param second other side of the connection
219  *
220  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
221  */
222 int
223 add_extended_topology (const struct GNUNET_PeerIdentity *first, const struct GNUNET_PeerIdentity *second)
224 {
225   int ret;
226   if (outfile == NULL)
227     return GNUNET_SYSERR;
228
229   if (first != NULL)
230     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_first_node;\n", GNUNET_h2s_full(&first->hashPubKey));
231   else
232     ret = fprintf(outfile, "set @temp_first_node = 0;\n");
233
234   if (ret < 0)
235     return GNUNET_SYSERR;
236
237   if (second != NULL)
238     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_second_node;\n", GNUNET_h2s_full(&second->hashPubKey));
239   else
240     ret = fprintf(outfile, "set @temp_second_node = 0;\n");
241
242   if (ret < 0)
243     return GNUNET_SYSERR;
244
245   ret = fprintf(outfile, "execute extend_topology using "
246                          "@temp_first_node, @temp_second_node;\n");
247
248   if (ret >= 0)
249     return GNUNET_OK;
250   return GNUNET_SYSERR;
251 }
252
253
254 /*
255  * Inserts the specified trial into the dhttests.trials table
256  *
257  * @param trialuid return the trialuid of the newly inserted trial
258  * @param num_nodes how many nodes are in the trial
259  * @param topology integer representing topology for this trial
260  * @param blacklist_topology integer representing blacklist topology for this trial
261  * @param connect_topology integer representing connect topology for this trial
262  * @param connect_topology_option integer representing connect topology option
263  * @param connect_topology_option_modifier float to modify connect option
264  * @param topology_percentage percentage modifier for certain topologies
265  * @param topology_probability probability modifier for certain topologies
266  * @param puts number of puts to perform
267  * @param gets number of gets to perform
268  * @param concurrent number of concurrent requests
269  * @param settle_time time to wait between creating topology and starting testing
270  * @param num_rounds number of times to repeat the trial
271  * @param malicious_getters number of malicious GET peers in the trial
272  * @param malicious_putters number of malicious PUT peers in the trial
273  * @param malicious_droppers number of malicious DROP peers in the trial
274  * @param malicious_get_frequency how often malicious gets are sent
275  * @param malicious_put_frequency how often malicious puts are sent
276  * @param stop_closest stop forwarding PUTs if closest node found
277  * @param stop_found stop forwarding GETs if data found
278  * @param strict_kademlia test used kademlia routing algorithm
279  * @param gets_succeeded how many gets did the test driver report success on
280  * @param message string to put into DB for this trial
281  *
282  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
283  */
284 int add_trial (unsigned long long *trialuid, unsigned int num_nodes, unsigned int topology,
285                unsigned int blacklist_topology, unsigned int connect_topology,
286                unsigned int connect_topology_option, float connect_topology_option_modifier,
287                float topology_percentage, float topology_probability,
288                unsigned int puts, unsigned int gets, unsigned int concurrent, unsigned int settle_time,
289                unsigned int num_rounds, unsigned int malicious_getters, unsigned int malicious_putters,
290                unsigned int malicious_droppers, unsigned int malicious_get_frequency,
291                unsigned int malicious_put_frequency, unsigned int stop_closest, unsigned int stop_found,
292                unsigned int strict_kademlia, unsigned int gets_succeeded,
293                char *message)
294 {
295   int ret;
296   if (trialuid != NULL)
297     *trialuid = 0;
298   if (outfile == NULL)
299     return GNUNET_SYSERR;
300
301   ret = fprintf(outfile, "set @date = \"%s\", @num = %u, @topology = %u, @bl = %u, "
302                    "@connect = %u, @c_t_o = %u, @c_t_o_m = %f, @t_p = %f, "
303                    "@t_pr = %f, @puts = %u, @gets = %u, "
304                    "@concurrent = %u, @settle = %u, @rounds = %u, "
305                    "@m_gets = %u, @m_puts = %u, @m_drops = %u, "
306                    "@m_g_f = %u, @m_p_f = %u, @s_c = %u, @s_f = %u,"
307                    "@s_k = %u, @g_s = %u, @message = \"%s\";\n",
308                    get_sql_time(), num_nodes, topology,
309                    blacklist_topology, connect_topology,
310                    connect_topology_option, connect_topology_option_modifier,
311                    topology_percentage, topology_probability,
312                    puts, gets, concurrent, settle_time,
313                    num_rounds, malicious_getters, malicious_putters,
314                    malicious_droppers, malicious_get_frequency, malicious_put_frequency,
315                    stop_closest, stop_found, strict_kademlia, gets_succeeded, message);
316
317   if (ret < 0)
318     return GNUNET_SYSERR;
319   ret = fprintf(outfile, "execute insert_trial using "
320                          "@date, @num, @topology, @t_p, @t_pr,"
321                          " @bl, @connect, @c_t_o,"
322                          "@c_t_o_m, @puts, @gets,"
323                          "@concurrent, @settle, @rounds,"
324                          "@m_gets, @m_puts, @m_drops,"
325                          "@m_g_f, @m_p_f, @s_c, @s_f,"
326                          "@s_k, @g_s, @message;\n");
327
328   ret = fprintf(outfile, "execute select_trial;\n");
329
330   if (ret >= 0)
331     return GNUNET_OK;
332   return GNUNET_SYSERR;
333 }
334
335
336 /*
337  * Inserts the specified stats into the dhttests.generic_stats table
338  *
339  * @param peer the peer inserting the statistic
340  * @param name the name of the statistic
341  * @param section the section of the statistic
342  * @param value the value of the statistic
343  *
344  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
345  */
346 int
347 add_generic_stat (const struct GNUNET_PeerIdentity *peer,
348                   const char *name,
349                   const char *section, uint64_t value)
350 {
351   int ret;
352   if (outfile == NULL)
353     return GNUNET_SYSERR;
354
355   if (peer != NULL)
356     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_node;\n", GNUNET_h2s_full(&peer->hashPubKey));
357   else
358     ret = fprintf(outfile, "set @temp_node = 0;\n");
359
360   ret = fprintf(outfile, "set @temp_section = \"%s\", @temp_stat = \"%s\", @temp_value = %llu;\n",
361                          section, name, (unsigned long long)value);
362
363   if (ret < 0)
364     return GNUNET_SYSERR;
365
366   ret = fprintf(outfile, "execute insert_generic_stat;\n");
367
368   return GNUNET_OK;
369 }
370
371
372 /*
373  * Inserts the specified stats into the dhttests.node_statistics table
374  *
375  * @param peer the peer inserting the statistic
376  * @param route_requests route requests seen
377  * @param route_forwards route requests forwarded
378  * @param result_requests route result requests seen
379  * @param client_requests client requests initiated
380  * @param result_forwards route results forwarded
381  * @param gets get requests handled
382  * @param puts put requests handle
383  * @param data_inserts data inserted at this node
384  * @param find_peer_requests find peer requests seen
385  * @param find_peers_started find peer requests initiated at this node
386  * @param gets_started get requests initiated at this node
387  * @param puts_started put requests initiated at this node
388  * @param find_peer_responses_received find peer responses received locally
389  * @param get_responses_received get responses received locally
390  * @param find_peer_responses_sent find peer responses sent from this node
391  * @param get_responses_sent get responses sent from this node
392  *
393  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
394  */
395 int
396 add_stat (const struct GNUNET_PeerIdentity *peer, unsigned int route_requests,
397           unsigned int route_forwards, unsigned int result_requests,
398           unsigned int client_requests, unsigned int result_forwards,
399           unsigned int gets, unsigned int puts,
400           unsigned int data_inserts, unsigned int find_peer_requests,
401           unsigned int find_peers_started, unsigned int gets_started,
402           unsigned int puts_started, unsigned int find_peer_responses_received,
403           unsigned int get_responses_received, unsigned int find_peer_responses_sent,
404           unsigned int get_responses_sent)
405 {
406   int ret;
407   if (outfile == NULL)
408     return GNUNET_SYSERR;
409
410   if (peer != NULL)
411     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_node;\n", GNUNET_h2s_full(&peer->hashPubKey));
412   else
413     ret = fprintf(outfile, "set @temp_node = 0;\n");
414
415   ret = fprintf(outfile, "set @r_r = %u, @r_f = %u, @res_r = %u, @c_r = %u, "
416                          "@res_f = %u, @gets = %u, @puts = %u, @d_i = %u, "
417                          "@f_p_r = %u, @f_p_s = %u, @g_s = %u, @p_s = %u, "
418                          "@f_p_r_r = %u, @g_r_r = %u, @f_p_r_s = %u, @g_r_s = %u;\n",
419                          route_requests, route_forwards, result_requests,
420                          client_requests, result_forwards, gets, puts,
421                          data_inserts, find_peer_requests, find_peers_started,
422                          gets_started, puts_started, find_peer_responses_received,
423                          get_responses_received, find_peer_responses_sent,
424                          get_responses_sent);
425
426   if (ret < 0)
427     return GNUNET_SYSERR;
428
429   ret = fprintf(outfile, "execute insert_stat using "
430                          "@temp_trial, @temp_node, @r_r, @r_f, @res_r, @c_r, "
431                          "@res_f, @gets, @puts, @d_i, "
432                          "@f_p_r, @f_p_s, @g_s, @p_s, "
433                          "@f_p_r_r, @g_r_r, @f_p_r_s, @g_r_s;\n");
434
435   return GNUNET_OK;
436 }
437 /*
438  * Inserts the specified dhtkey into the dhttests.dhtkeys table,
439  * stores return value of dhttests.dhtkeys.dhtkeyuid into dhtkeyuid
440  *
441  * @param dhtkeyuid return value
442  * @param dhtkey hashcode of key to insert
443  *
444  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
445  */
446 int
447 add_dhtkey (unsigned long long *dhtkeyuid, const GNUNET_HashCode * dhtkey)
448 {
449   int ret;
450   if (dhtkeyuid != NULL)
451     *dhtkeyuid = 0;
452
453   if (outfile == NULL)
454     return GNUNET_SYSERR;
455
456   if (dhtkey != NULL)
457     ret = fprintf(outfile, "set @dhtkey = \"%s\";\n", GNUNET_h2s_full(dhtkey));
458   else
459     ret = fprintf(outfile, "set @dhtkey = XXXXX;\n");
460
461   if (ret < 0)
462     return GNUNET_SYSERR;
463   ret = fprintf(outfile, "execute insert_dhtkey using @dhtkey;\n");
464
465   if (ret >= 0)
466     return GNUNET_OK;
467   return GNUNET_SYSERR;
468 }
469
470 /*
471  * Inserts the specified node into the dhttests.nodes table
472  *
473  * @param nodeuid the inserted node uid
474  * @param node the node to insert
475  *
476  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
477  */
478 int
479 add_node (unsigned long long *nodeuid, struct GNUNET_PeerIdentity * node)
480 {
481   int ret;
482
483   if (node == NULL)
484     return GNUNET_SYSERR;
485
486   if (outfile == NULL)
487     return GNUNET_SYSERR;
488
489   if (node != NULL)
490     ret = fprintf(outfile, "set @node = \"%s\";\n", GNUNET_h2s_full(&node->hashPubKey));
491   else
492     return GNUNET_SYSERR;
493
494   if (ret < 0)
495     return GNUNET_SYSERR;
496
497   ret = fprintf(outfile, "execute insert_node using @node;\n");
498
499   if (ret >= 0)
500     return GNUNET_OK;
501   return GNUNET_SYSERR;
502 }
503
504 /*
505  * Update dhttests.trials table with current server time as end time
506  *
507  * @param trialuid trial to update
508  * @param gets_succeeded how many gets did the testcase report as successful
509  *
510  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
511  */
512 int
513 update_trials (unsigned long long trialuid,
514                unsigned int gets_succeeded)
515 {
516   int ret;
517 #if DEBUG_DHTLOG
518   if (trialuid != current_trial)
519     {
520       fprintf (stderr,
521                _("Trialuid to update is not equal to current_trial\n"));
522     }
523 #endif
524
525   if (outfile == NULL)
526     return GNUNET_SYSERR;
527
528   ret = fprintf(outfile, "set @date = \"%s\", @g_s = %u;\n", get_sql_time(), gets_succeeded);
529
530   if (ret < 0)
531     return GNUNET_SYSERR;
532
533   ret = fprintf(outfile, "execute update_trial using @date, @g_s;\n");
534
535   if (ret >= 0)
536     return GNUNET_OK;
537   else
538     return GNUNET_SYSERR;
539 }
540
541
542 /*
543  * Update dhttests.nodes table setting the identified
544  * node as a malicious dropper.
545  *
546  * @param peer the peer that was set to be malicious
547  *
548  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
549  */
550 int
551 set_malicious (struct GNUNET_PeerIdentity *peer)
552 {
553   int ret;
554
555   if (outfile == NULL)
556     return GNUNET_SYSERR;
557
558   ret = fprintf(outfile, "set @temp_node = \"%s\";\n", GNUNET_h2s_full(&peer->hashPubKey));
559
560   if (ret < 0)
561     return GNUNET_SYSERR;
562
563   ret = fprintf(outfile, "execute set_malicious;\n");
564
565   if (ret >= 0)
566     return GNUNET_OK;
567   else
568     return GNUNET_SYSERR;
569 }
570
571
572 /*
573  * Update dhttests.trials table with total connections information
574  *
575  * @param trialuid the trialuid to update
576  * @param totalConnections the number of connections
577  *
578  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
579  */
580 int
581 add_connections (unsigned long long trialuid, unsigned int totalConnections)
582 {
583   int ret;
584 #if DEBUG_DHTLOG
585   if (trialuid != current_trial)
586     {
587       fprintf (stderr,
588                _("Trialuid to update is not equal to current_trial(!)(?)\n"));
589     }
590 #endif
591   if (outfile == NULL)
592     return GNUNET_SYSERR;
593
594   ret = fprintf(outfile, "set @conns = %u;\n", totalConnections);
595
596   if (ret < 0)
597     return GNUNET_SYSERR;
598
599   ret = fprintf(outfile, "execute update_conn using @conns;\n");
600
601   if (ret >= 0)
602     return GNUNET_OK;
603   else
604     return GNUNET_SYSERR;
605 }
606
607
608 /*
609  * Update dhttests.topology table with total connections information
610  *
611  * @param totalConnections the number of connections
612  *
613  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
614  */
615 int
616 update_topology (unsigned int connections)
617 {
618   int ret;
619   if (outfile == NULL)
620     return GNUNET_SYSERR;
621
622   ret = fprintf(outfile, "set @temp_conns = %u;\n", connections);
623
624   if (ret < 0)
625     return GNUNET_SYSERR;
626
627   ret = fprintf(outfile, "execute update_topology using @temp_conns;\n");
628
629   if (ret >= 0)
630     return GNUNET_OK;
631   else
632     return GNUNET_SYSERR;
633 }
634
635 /*
636  * Inserts the specified query into the dhttests.queries table
637  *
638  * @param sqlqueruid inserted query uid
639  * @param queryid dht query id
640  * @param type type of the query
641  * @param hops number of hops query traveled
642  * @param succeeded whether or not query was successful
643  * @param node the node the query hit
644  * @param key the key of the query
645  *
646  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
647  */
648 int
649 add_query (unsigned long long *sqlqueryuid, unsigned long long queryid,
650            unsigned int type, unsigned int hops, int succeeded,
651            const struct GNUNET_PeerIdentity * node, const GNUNET_HashCode * key)
652 {
653   int ret;
654
655   if (outfile == NULL)
656     return GNUNET_SYSERR;
657
658   if (sqlqueryuid != NULL)
659     *sqlqueryuid = 0;
660
661   if (key != NULL)
662     ret = fprintf(outfile, "select dhtkeyuid from dhtkeys where trialuid = @temp_trial and dhtkey = \"%s\" into @temp_dhtkey;\n", GNUNET_h2s_full(key));
663   else
664     ret = fprintf(outfile, "set @temp_dhtkey = 0;\n");
665
666   if (node != NULL)
667     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_node;\n", GNUNET_h2s_full(&node->hashPubKey));
668   else
669     ret = fprintf(outfile, "set @temp_node = 0;\n");
670
671   ret = fprintf(outfile, "set @qid = %llu, @type = %u, @hops = %u, @succ = %d;\n", queryid, type, hops, succeeded);
672
673   if (ret < 0)
674     return GNUNET_SYSERR;
675
676   ret = fprintf(outfile, "execute insert_query using @type, @hops, @temp_dhtkey, @qid, @succ, @temp_node;\n");
677
678   if (ret >= 0)
679     return GNUNET_OK;
680   else
681     return GNUNET_SYSERR;
682 }
683
684 /*
685  * Inserts the specified route information into the dhttests.routes table
686  *
687  * @param sqlqueruid inserted query uid
688  * @param queryid dht query id
689  * @param type type of the query
690  * @param hops number of hops query traveled
691  * @param succeeded whether or not query was successful
692  * @param node the node the query hit
693  * @param key the key of the query
694  * @param from_node the node that sent the message to node
695  * @param to_node next node to forward message to
696  *
697  * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
698  */
699 int
700 add_route (unsigned long long *sqlqueryuid, unsigned long long queryid,
701            unsigned int type, unsigned int hops,
702            int succeeded, const struct GNUNET_PeerIdentity * node,
703            const GNUNET_HashCode * key, const struct GNUNET_PeerIdentity * from_node,
704            const struct GNUNET_PeerIdentity * to_node)
705 {
706   int ret;
707
708   if (outfile == NULL)
709     return GNUNET_SYSERR;
710
711   if (sqlqueryuid != NULL)
712     *sqlqueryuid = 0;
713
714   if (key != NULL)
715     ret = fprintf(outfile, "select dhtkeyuid from dhtkeys where trialuid = @temp_trial and dhtkey = \"%s\" into @temp_dhtkey;\n", GNUNET_h2s_full(key));
716   else
717     ret = fprintf(outfile, "set @temp_dhtkey = 0;\n");
718
719   if (node != NULL)
720     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_node;\n", GNUNET_h2s_full(&node->hashPubKey));
721   else
722     ret = fprintf(outfile, "set @temp_node = 0;\n");
723
724   if (from_node != NULL)
725     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_from_node;\n", GNUNET_h2s_full(&from_node->hashPubKey));
726   else
727     ret = fprintf(outfile, "set @temp_from_node = 0;\n");
728
729   if (to_node != NULL)
730     ret = fprintf(outfile, "select nodeuid from nodes where trialuid = @temp_trial and nodeid = \"%s\" into @temp_to_node;\n", GNUNET_h2s_full(&to_node->hashPubKey));
731   else
732     ret = fprintf(outfile, "set @temp_to_node = 0;\n");
733
734   ret = fprintf(outfile, "set @qid = %llu, @type = %u, @hops = %u, @succ = %d;\n", queryid, type, hops, succeeded);
735
736   if (ret < 0)
737     return GNUNET_SYSERR;
738
739   ret = fprintf(outfile, "execute insert_route using @type, @hops, @temp_dhtkey, @qid, @succ, @temp_node, @temp_from_node, @temp_to_node;\n");
740
741   if (ret >= 0)
742     return GNUNET_OK;
743   else
744     return GNUNET_SYSERR;
745 }
746
747 /*
748  * Provides the dhtlog api
749  *
750  * @param c the configuration to use to connect to a server
751  *
752  * @return the handle to the server, or NULL on error
753  */
754 void *
755 libgnunet_plugin_dhtlog_mysql_dump_init (void * cls)
756 {
757   struct GNUNET_DHTLOG_Plugin *plugin = cls;
758   char *outfile_name;
759   char *outfile_path;
760   char *fn;
761   int dirwarn;
762
763   cfg = plugin->cfg;
764   max_varchar_len = 255;
765
766   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MySQL (DUMP) DHT Logger: initializing\n");
767
768   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (plugin->cfg,
769                                                          "MYSQLDUMP", "PATH",
770                                                          &outfile_path))
771     {
772       outfile_path = GNUNET_strdup("");
773     }
774
775   GNUNET_asprintf (&outfile_name,
776                    "%s%s-%d",
777                    outfile_path,
778                    "mysqldump",
779                    getpid());
780
781   fn = GNUNET_STRINGS_filename_expand (outfile_name);
782
783   dirwarn = (GNUNET_OK !=  GNUNET_DISK_directory_create_for_file (fn));
784   outfile = FOPEN (fn, "w");
785
786   if (outfile == NULL)
787     {
788       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
789       if (dirwarn)
790         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
791                     _("Failed to create or access directory for log file `%s'\n"),
792                     fn);
793       GNUNET_free(outfile_path);
794       GNUNET_free(outfile_name);
795       GNUNET_free (fn);
796       return NULL;
797     }
798
799   GNUNET_free (outfile_path);
800   GNUNET_free (outfile_name);
801   GNUNET_free (fn);
802
803   if (iopen () != GNUNET_OK)
804     {
805       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
806                   _("Failed to create file for dhtlog.\n"));
807       return NULL;
808     }
809   GNUNET_assert(plugin->dhtlog_api == NULL);
810   plugin->dhtlog_api = GNUNET_malloc(sizeof(struct GNUNET_DHTLOG_Handle));
811   plugin->dhtlog_api->insert_trial = &add_trial;
812   plugin->dhtlog_api->insert_stat = &add_stat;
813   plugin->dhtlog_api->insert_query = &add_query;
814   plugin->dhtlog_api->update_trial = &update_trials;
815   plugin->dhtlog_api->insert_route = &add_route;
816   plugin->dhtlog_api->insert_node = &add_node;
817   plugin->dhtlog_api->insert_dhtkey = &add_dhtkey;
818   plugin->dhtlog_api->update_connections = &add_connections;
819   plugin->dhtlog_api->insert_topology = &add_topology;
820   plugin->dhtlog_api->insert_extended_topology = &add_extended_topology;
821   plugin->dhtlog_api->update_topology = &update_topology;
822   plugin->dhtlog_api->set_malicious = &set_malicious;
823   plugin->dhtlog_api->add_generic_stat = &add_generic_stat;
824
825   return plugin;
826 }
827
828 /**
829  * Shutdown the plugin.
830  */
831 void *
832 libgnunet_plugin_dhtlog_mysql_dump_done (void * cls)
833 {
834   struct GNUNET_DHTLOG_Handle *dhtlog_api = cls;
835   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836               "MySQL DHT Logger: database shutdown\n");
837   GNUNET_assert(dhtlog_api != NULL);
838
839   GNUNET_free(dhtlog_api);
840   return NULL;
841 }
842
843 /* end of plugin_dhtlog_mysql.c */