2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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 3, or (at your
8 * option) any later version.
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.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file psycstore/plugin_psycstore_mysql.c
23 * @brief mysql-based psycstore backend
24 * @author Gabor X Toth
25 * @author Christian Grothoff
26 * @author Christophe Genevey
30 #include "gnunet_psycstore_plugin.h"
31 #include "gnunet_psycstore_service.h"
32 #include "gnunet_multicast_service.h"
33 #include "gnunet_crypto_lib.h"
34 #include "gnunet_psyc_util_lib.h"
35 #include "psycstore.h"
36 #include "gnunet_my_lib.h"
37 #include "gnunet_mysql_lib.h"
38 #include <mysql/mysql.h>
41 * After how many ms "busy" should a DB operation fail for good? A
42 * low value makes sure that we are more responsive to requests
43 * (especially PUTs). A high value guarantees a higher success rate
44 * (SELECTs in iterate can take several seconds despite LIMIT=1).
46 * The default value of 1s should ensure that users do not experience
47 * huge latencies while at the same time allowing operations to
48 * succeed with reasonable probability.
50 #define BUSY_TIMEOUT_MS 1000
52 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
55 * Log an error message at log-level 'level' that indicates
56 * a failure of the command 'cmd' on file 'filename'
57 * with the message given by strerror(errno).
59 #define LOG_MYSQL(db, level, cmd, stmt) do { GNUNET_log_from (level, "psycstore-mysql", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_stmt_error (GNUNET_MYSQL_statement_get_stmt(stmt))); } while(0)
61 #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-mysql", __VA_ARGS__)
65 TRANSACTION_STATE_MODIFY,
66 TRANSACTION_STATE_SYNC,
70 * Context for all functions in this plugin.
75 const struct GNUNET_CONFIGURATION_Handle *cfg;
83 *Handle to talk to Mysql
85 struct GNUNET_MYSQL_Context *mc;
88 * Current transaction.
90 enum Transactions transaction;
92 struct GNUNET_MYSQL_StatementHandle *transaction_begin;
94 struct GNUNET_MYSQL_StatementHandle *transaction_commit;
96 struct GNUNET_MYSQL_StatementHandle *transaction_rollback;
99 * Precompiled SQL for channel_key_store()
101 struct GNUNET_MYSQL_StatementHandle *insert_channel_key;
105 * Precompiled SQL for slave_key_store()
107 struct GNUNET_MYSQL_StatementHandle *insert_slave_key;
110 * Precompiled SQL for membership_store()
112 struct GNUNET_MYSQL_StatementHandle *insert_membership;
115 * Precompiled SQL for membership_test()
117 struct GNUNET_MYSQL_StatementHandle *select_membership;
120 * Precompiled SQL for fragment_store()
122 struct GNUNET_MYSQL_StatementHandle *insert_fragment;
125 * Precompiled SQL for message_add_flags()
127 struct GNUNET_MYSQL_StatementHandle *update_message_flags;
130 * Precompiled SQL for fragment_get()
132 struct GNUNET_MYSQL_StatementHandle *select_fragments;
135 * Precompiled SQL for fragment_get()
137 struct GNUNET_MYSQL_StatementHandle *select_latest_fragments;
140 * Precompiled SQL for message_get()
142 struct GNUNET_MYSQL_StatementHandle *select_messages;
145 * Precompiled SQL for message_get()
147 struct GNUNET_MYSQL_StatementHandle *select_latest_messages;
150 * Precompiled SQL for message_get_fragment()
152 struct GNUNET_MYSQL_StatementHandle *select_message_fragment;
155 * Precompiled SQL for counters_get_message()
157 struct GNUNET_MYSQL_StatementHandle *select_counters_message;
160 * Precompiled SQL for counters_get_state()
162 struct GNUNET_MYSQL_StatementHandle *select_counters_state;
165 * Precompiled SQL for state_modify_end()
167 struct GNUNET_MYSQL_StatementHandle *update_state_hash_message_id;
170 * Precompiled SQL for state_sync_end()
172 struct GNUNET_MYSQL_StatementHandle *update_max_state_message_id;
175 * Precompiled SQL for state_modify_op()
177 struct GNUNET_MYSQL_StatementHandle *insert_state_current;
180 * Precompiled SQL for state_modify_end()
182 struct GNUNET_MYSQL_StatementHandle *delete_state_empty;
185 * Precompiled SQL for state_set_signed()
187 struct GNUNET_MYSQL_StatementHandle *update_state_signed;
190 * Precompiled SQL for state_sync()
192 struct GNUNET_MYSQL_StatementHandle *insert_state_sync;
195 * Precompiled SQL for state_sync()
197 struct GNUNET_MYSQL_StatementHandle *delete_state;
200 * Precompiled SQL for state_sync()
202 struct GNUNET_MYSQL_StatementHandle *insert_state_from_sync;
205 * Precompiled SQL for state_sync()
207 struct GNUNET_MYSQL_StatementHandle *delete_state_sync;
210 * Precompiled SQL for state_get_signed()
212 struct GNUNET_MYSQL_StatementHandle *select_state_signed;
215 * Precompiled SQL for state_get()
217 struct GNUNET_MYSQL_StatementHandle *select_state_one;
220 * Precompiled SQL for state_get_prefix()
222 struct GNUNET_MYSQL_StatementHandle *select_state_prefix;
229 mysql_trace (void *cls, const char *sql)
231 LOG(GNUNET_ERROR_TYPE_DEBUG, "MYSQL query:\n%s\n", sql);
238 * @brief Prepare a SQL statement
240 * @param dbh handle to the database
241 * @param sql SQL statement, UTF-8 encoded
242 * @param stmt set to the prepared statement
243 * @return 0 on success
246 mysql_prepare (struct GNUNET_MYSQL_Context *mc,
248 struct GNUNET_MYSQL_StatementHandle **stmt)
250 *stmt = GNUNET_MYSQL_statement_prepare (mc,
253 LOG(GNUNET_ERROR_TYPE_DEBUG,
254 "Prepared `%s' / %p\n", sql, stmt);
256 LOG(GNUNET_ERROR_TYPE_ERROR,
257 _("Error preparing SQL query: %s\n %s\n"),
258 mysql_stmt_error (GNUNET_MYSQL_statement_get_stmt (*stmt)), sql);
265 * Initialize the database connections and associated
266 * data structures (create tables and indices
267 * as needed as well).
269 * @param plugin the plugin context (state for this module)
270 * @return GNUNET_OK on success
273 database_setup (struct Plugin *plugin)
278 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "psycstore-mysql",
279 "FILENAME", &filename))
281 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
282 "psycstore-mysql", "FILENAME");
283 return GNUNET_SYSERR;
286 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
288 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (filename))
291 GNUNET_free (filename);
292 return GNUNET_SYSERR;
295 /* filename should be UTF-8-encoded. If it isn't, it's a bug */
296 plugin->fn = filename;
298 /* Open database and precompile statements */
299 plugin->mc = GNUNET_MYSQL_context_create(plugin->cfg, "psycstore-mysql");
301 if (NULL == plugin->mc)
303 LOG(GNUNET_ERROR_TYPE_ERROR,
304 _("Unable to initialize Mysql.\n"));
305 return GNUNET_SYSERR;
310 GNUNET_MYSQL_statement_run (plugin->mc,
311 "CREATE TABLE IF NOT EXISTS channels (\n"
312 " id INT AUTO_INCREMENT,\n"
314 " max_state_message_id INT,\n"
315 " state_hash_message_id INT,\n"
316 " PRIMARY KEY(id),\n"
317 " UNIQUE KEY(pub_key(5))\n"
320 GNUNET_MYSQL_statement_run (plugin->mc,
321 "CREATE TABLE IF NOT EXISTS slaves (\n"
322 " id INT AUTO_INCREMENT,\n"
324 " PRIMARY KEY(id),\n"
325 " UNIQUE KEY(pub_key(5))\n"
328 GNUNET_MYSQL_statement_run (plugin->mc,
329 "CREATE TABLE IF NOT EXISTS membership (\n"
330 " channel_id INT NOT NULL REFERENCES channels(id),\n"
331 " slave_id INT NOT NULL REFERENCES slaves(id),\n"
332 " did_join INT NOT NULL,\n"
333 " announced_at BIGINT UNSIGNED NOT NULL,\n"
334 " effective_since BIGINT UNSIGNED NOT NULL,\n"
335 " group_generation BIGINT UNSIGNED NOT NULL\n"
338 /*** FIX because IF NOT EXISTS doesn't work ***/
339 GNUNET_MYSQL_statement_run (plugin->mc,
340 "CREATE INDEX idx_membership_channel_id_slave_id "
341 "ON membership (channel_id, slave_id);");
343 /** @todo messages table: add method_name column */
344 GNUNET_MYSQL_statement_run (plugin->mc,
345 "CREATE TABLE IF NOT EXISTS messages (\n"
346 " channel_id INT NOT NULL REFERENCES channels(id),\n"
347 " hop_counter BIGINT UNSIGNED NOT NULL,\n"
350 " fragment_id BIGINT UNSIGNED NOT NULL,\n"
351 " fragment_offset BIGINT UNSIGNED NOT NULL,\n"
352 " message_id BIGINT UNSIGNED NOT NULL,\n"
353 " group_generation BIGINT UNSIGNED NOT NULL,\n"
354 " multicast_flags BIGINT UNSIGNED NOT NULL,\n"
355 " psycstore_flags BIGINT UNSIGNED NOT NULL,\n"
357 " PRIMARY KEY (channel_id, fragment_id),\n"
358 " UNIQUE KEY(channel_id, message_id, fragment_offset)\n"
361 GNUNET_MYSQL_statement_run (plugin->mc,
362 "CREATE TABLE IF NOT EXISTS state (\n"
363 " channel_id INT NOT NULL REFERENCES channels(id),\n"
364 " name TEXT NOT NULL,\n"
365 " value_current BLOB,\n"
366 " value_signed BLOB,\n"
367 " PRIMARY KEY (channel_id, name(5))\n"
370 GNUNET_MYSQL_statement_run (plugin->mc,
371 "CREATE TABLE IF NOT EXISTS state_sync (\n"
372 " channel_id INT NOT NULL REFERENCES channels(id),\n"
373 " name TEXT NOT NULL,\n"
375 " PRIMARY KEY (channel_id, name(5))\n"
378 /* Prepare statements */
379 mysql_prepare (plugin->mc,
381 &plugin->transaction_begin);
383 mysql_prepare (plugin->mc,
385 &plugin->transaction_commit);
387 mysql_prepare (plugin->mc,
389 &plugin->transaction_rollback);
391 mysql_prepare (plugin->mc,
392 "INSERT IGNORE INTO channels (pub_key) VALUES (?);",
393 &plugin->insert_channel_key);
395 mysql_prepare (plugin->mc,
396 "INSERT IGNORE INTO slaves (pub_key) VALUES (?);",
397 &plugin->insert_slave_key);
399 mysql_prepare (plugin->mc,
400 "INSERT INTO membership\n"
401 " (channel_id, slave_id, did_join, announced_at,\n"
402 " effective_since, group_generation)\n"
403 "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n"
404 " (SELECT id FROM slaves WHERE pub_key = ?),\n"
406 &plugin->insert_membership);
408 mysql_prepare (plugin->mc,
409 "SELECT did_join FROM membership\n"
410 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
411 " AND slave_id = (SELECT id FROM slaves WHERE pub_key = ?)\n"
412 " AND effective_since <= ? AND did_join = 1\n"
413 "ORDER BY announced_at DESC LIMIT 1;",
414 &plugin->select_membership);
416 mysql_prepare (plugin->mc,
417 "INSERT IGNORE INTO messages\n"
418 " (channel_id, hop_counter, signature, purpose,\n"
419 " fragment_id, fragment_offset, message_id,\n"
420 " group_generation, multicast_flags, psycstore_flags, data)\n"
421 "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n"
422 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
423 &plugin->insert_fragment);
425 mysql_prepare (plugin->mc,
427 "SET psycstore_flags = psycstore_flags | ?\n"
428 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
429 " AND message_id = ? AND fragment_offset = 0;",
430 &plugin->update_message_flags);
432 mysql_prepare (plugin->mc,
433 "SELECT hop_counter, signature, purpose, fragment_id,\n"
434 " fragment_offset, message_id, group_generation,\n"
435 " multicast_flags, psycstore_flags, data\n"
437 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
438 " AND ? <= fragment_id AND fragment_id <= ?;",
439 &plugin->select_fragments);
441 /** @todo select_messages: add method_prefix filter */
442 mysql_prepare (plugin->mc,
443 "SELECT hop_counter, signature, purpose, fragment_id,\n"
444 " fragment_offset, message_id, group_generation,\n"
445 " multicast_flags, psycstore_flags, data\n"
447 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
448 " AND ? <= message_id AND message_id <= ?"
450 &plugin->select_messages);
452 mysql_prepare (plugin->mc,
454 "(SELECT hop_counter, signature, purpose, fragment_id,\n"
455 " fragment_offset, message_id, group_generation,\n"
456 " multicast_flags, psycstore_flags, data\n"
458 " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
459 " ORDER BY fragment_id DESC\n"
461 "ORDER BY fragment_id;",
462 &plugin->select_latest_fragments);
464 /** @todo select_latest_messages: add method_prefix filter */
465 mysql_prepare (plugin->mc,
466 "SELECT hop_counter, signature, purpose, fragment_id,\n"
467 " fragment_offset, message_id, group_generation,\n"
468 " multicast_flags, psycstore_flags, data\n"
470 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
471 " AND message_id IN\n"
472 " (SELECT message_id\n"
474 " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
475 " GROUP BY message_id\n"
476 " ORDER BY message_id\n"
478 "ORDER BY fragment_id;",
479 &plugin->select_latest_messages);
481 mysql_prepare (plugin->mc,
482 "SELECT hop_counter, signature, purpose, fragment_id,\n"
483 " fragment_offset, message_id, group_generation,\n"
484 " multicast_flags, psycstore_flags, data\n"
486 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
487 " AND message_id = ? AND fragment_offset = ?;",
488 &plugin->select_message_fragment);
490 mysql_prepare (plugin->mc,
491 "SELECT fragment_id, message_id, group_generation\n"
493 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
494 "ORDER BY fragment_id DESC LIMIT 1;",
495 &plugin->select_counters_message);
497 mysql_prepare (plugin->mc,
498 "SELECT max_state_message_id\n"
500 "WHERE pub_key = ? AND max_state_message_id IS NOT NULL;",
501 &plugin->select_counters_state);
503 mysql_prepare (plugin->mc,
505 "SET max_state_message_id = ?\n"
506 "WHERE pub_key = ?;",
507 &plugin->update_max_state_message_id);
509 mysql_prepare (plugin->mc,
511 "SET state_hash_message_id = ?\n"
512 "WHERE pub_key = ?;",
513 &plugin->update_state_hash_message_id);
515 mysql_prepare (plugin->mc,
516 "REPLACE INTO state\n"
517 " (channel_id, name, value_current, value_signed)\n"
518 "SELECT new.channel_id, new.name,\n"
519 " new.value_current, old.value_signed\n"
520 "FROM (SELECT (SELECT id FROM channels WHERE pub_key = ?)\n"
522 " ? AS name, ? AS value_current) AS new\n"
523 "LEFT JOIN (SELECT channel_id, name, value_signed\n"
524 " FROM state) AS old\n"
525 "ON new.channel_id = old.channel_id AND new.name = old.name;",
526 &plugin->insert_state_current);
528 mysql_prepare (plugin->mc,
529 "DELETE FROM state\n"
530 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
531 " AND (value_current IS NULL OR length(value_current) = 0)\n"
532 " AND (value_signed IS NULL OR length(value_signed) = 0);",
533 &plugin->delete_state_empty);
535 mysql_prepare (plugin->mc,
537 "SET value_signed = value_current\n"
538 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
539 &plugin->update_state_signed);
541 mysql_prepare (plugin->mc,
542 "DELETE FROM state\n"
543 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
544 &plugin->delete_state);
546 mysql_prepare (plugin->mc,
547 "INSERT INTO state_sync (channel_id, name, value)\n"
548 "VALUES ((SELECT id FROM channels WHERE pub_key = ?), ?, ?);",
549 &plugin->insert_state_sync);
551 mysql_prepare (plugin->mc,
552 "INSERT INTO state\n"
553 " (channel_id, name, value_current, value_signed)\n"
554 "SELECT channel_id, name, value, value\n"
556 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
557 &plugin->insert_state_from_sync);
559 mysql_prepare (plugin->mc,
560 "DELETE FROM state_sync\n"
561 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);",
562 &plugin->delete_state_sync);
564 mysql_prepare (plugin->mc,
565 "SELECT value_current\n"
567 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
569 &plugin->select_state_one);
571 mysql_prepare (plugin->mc,
572 "SELECT name, value_current\n"
574 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
575 " AND (name = ? OR substr(name, 1, ?) = ? || '_');",
576 &plugin->select_state_prefix);
578 mysql_prepare (plugin->mc,
579 "SELECT name, value_signed\n"
581 "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)"
582 " AND value_signed IS NOT NULL;",
583 &plugin->select_state_signed);
590 * Shutdown database connection and associate data
592 * @param plugin the plugin context (state for this module)
595 database_shutdown (struct Plugin *plugin)
597 GNUNET_MYSQL_context_destroy (plugin->mc);
599 GNUNET_free_non_null (plugin->fn);
605 * Execute a prepared statement with a @a channel_key argument.
607 * @param plugin Plugin handle.
608 * @param stmt Statement to execute.
609 * @param channel_key Public key of the channel.
611 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
614 exec_channel (struct Plugin *plugin, struct GNUNET_MYSQL_StatementHandle *stmt,
615 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
617 struct GNUNET_MY_QueryParam params[] = {
618 GNUNET_MY_query_param_auto_from_type (channel_key),
619 GNUNET_MY_query_param_end
622 if(GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
626 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
627 "mysql exec_channel", stmt);
630 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
632 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
633 "mysql_stmt_reset", stmt);
634 return GNUNET_SYSERR;
642 * Begin a transaction.
645 transaction_begin (struct Plugin *plugin, enum Transactions transaction)
647 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->transaction_begin;
649 struct GNUNET_MY_QueryParam params[] = {
650 GNUNET_MY_query_param_end
653 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
657 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
658 "mysql extract_result", stmt);
659 return GNUNET_SYSERR;
662 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt(stmt)))
664 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
665 "mysql_stmt_reset", stmt);
666 return GNUNET_SYSERR;
669 plugin->transaction = transaction;
675 * Commit current transaction.
678 transaction_commit (struct Plugin *plugin)
680 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->transaction_commit;
682 struct GNUNET_MY_QueryParam params[] = {
683 GNUNET_MY_query_param_end
686 if (GNUNET_OK != GNUNET_MY_exec_prepared( plugin->mc,
690 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
691 "mysql extract_result", stmt);
692 return GNUNET_SYSERR;
695 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
697 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
698 "mysql_stmt_reset", stmt);
699 return GNUNET_SYSERR;
702 plugin->transaction = TRANSACTION_NONE;
708 * Roll back current transaction.
711 transaction_rollback (struct Plugin *plugin)
713 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->transaction_rollback;
715 struct GNUNET_MY_QueryParam params[] = {
716 GNUNET_MY_query_param_end
719 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
723 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
724 "mysql extract_result", stmt);
725 return GNUNET_SYSERR;
728 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
730 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
731 "mysql_stmt_reset", stmt);
732 return GNUNET_SYSERR;
735 plugin->transaction = TRANSACTION_NONE;
741 channel_key_store (struct Plugin *plugin,
742 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
744 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_channel_key;
746 struct GNUNET_MY_QueryParam params[] = {
747 GNUNET_MY_query_param_auto_from_type (channel_key),
748 GNUNET_MY_query_param_end
751 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
755 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
756 "mysql exec_prepared", stmt);
757 return GNUNET_SYSERR;
760 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
762 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
763 "mysql_stmt_reset", stmt);
764 return GNUNET_SYSERR;
772 slave_key_store (struct Plugin *plugin,
773 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key)
775 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_slave_key;
777 struct GNUNET_MY_QueryParam params[] = {
778 GNUNET_MY_query_param_auto_from_type (slave_key),
779 GNUNET_MY_query_param_end
782 if (GNUNET_OK != GNUNET_MY_exec_prepared( plugin->mc,
786 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
787 "mysql exec_prepared", stmt);
788 return GNUNET_SYSERR;
791 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
793 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
794 "mysql_stmt_reset", stmt);
795 return GNUNET_SYSERR;
803 * Store join/leave events for a PSYC channel in order to be able to answer
804 * membership test queries later.
806 * @see GNUNET_PSYCSTORE_membership_store()
808 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
811 mysql_membership_store (void *cls,
812 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
813 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
815 uint64_t announced_at,
816 uint64_t effective_since,
817 uint64_t group_generation)
819 struct Plugin *plugin = cls;
821 uint32_t idid_join = (uint32_t)did_join;
823 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_membership;
825 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
827 if (announced_at > INT64_MAX ||
828 effective_since > INT64_MAX ||
829 group_generation > INT64_MAX)
832 return GNUNET_SYSERR;
835 if (GNUNET_OK != channel_key_store (plugin, channel_key)
836 || GNUNET_OK != slave_key_store (plugin, slave_key))
837 return GNUNET_SYSERR;
839 struct GNUNET_MY_QueryParam params[] = {
840 GNUNET_MY_query_param_auto_from_type (channel_key),
841 GNUNET_MY_query_param_auto_from_type (slave_key),
842 GNUNET_MY_query_param_uint32 (&idid_join),
843 GNUNET_MY_query_param_uint64 (&announced_at),
844 GNUNET_MY_query_param_uint64 (&effective_since),
845 GNUNET_MY_query_param_uint64 (&group_generation),
846 GNUNET_MY_query_param_end
849 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
853 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
854 "mysql exec_prepared", stmt);
855 return GNUNET_SYSERR;
858 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
860 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
861 "mysql_stmt_reset", stmt);
862 return GNUNET_SYSERR;
868 * Test if a member was admitted to the channel at the given message ID.
870 * @see GNUNET_PSYCSTORE_membership_test()
872 * @return #GNUNET_YES if the member was admitted, #GNUNET_NO if not,
873 * #GNUNET_SYSERR if there was en error.
876 membership_test (void *cls,
877 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
878 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
881 struct Plugin *plugin = cls;
883 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_membership;
885 uint32_t did_join = 0;
887 int ret = GNUNET_SYSERR;
889 struct GNUNET_MY_QueryParam params_select[] = {
890 GNUNET_MY_query_param_auto_from_type (channel_key),
891 GNUNET_MY_query_param_auto_from_type (slave_key),
892 GNUNET_MY_query_param_uint64 (&message_id),
893 GNUNET_MY_query_param_end
896 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
900 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
901 "mysql execute prepared", stmt);
902 return GNUNET_SYSERR;
905 struct GNUNET_MY_ResultSpec results_select[] = {
906 GNUNET_MY_result_spec_uint32 (&did_join),
907 GNUNET_MY_result_spec_end
910 switch(GNUNET_MY_extract_result (stmt,
920 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
921 "mysql extract_result", stmt);
922 return GNUNET_SYSERR;
925 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
927 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
928 "mysql_stmt_reset", stmt);
929 return GNUNET_SYSERR;
936 * Store a message fragment sent to a channel.
938 * @see GNUNET_PSYCSTORE_fragment_store()
940 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
943 fragment_store (void *cls,
944 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
945 const struct GNUNET_MULTICAST_MessageHeader *msg,
946 uint32_t psycstore_flags)
948 struct Plugin *plugin = cls;
950 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->insert_fragment;
952 GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
954 uint64_t fragment_id = GNUNET_ntohll (msg->fragment_id);
956 uint64_t fragment_offset = GNUNET_ntohll (msg->fragment_offset);
957 uint64_t message_id = GNUNET_ntohll (msg->message_id);
958 uint64_t group_generation = GNUNET_ntohll (msg->group_generation);
960 uint64_t hop_counter = ntohl(msg->hop_counter);
961 uint64_t flags = ntohl(msg->flags);
963 if (fragment_id > INT64_MAX || fragment_offset > INT64_MAX ||
964 message_id > INT64_MAX || group_generation > INT64_MAX)
966 LOG(GNUNET_ERROR_TYPE_ERROR,
967 "Tried to store fragment with a field > INT64_MAX: "
968 "%lu, %lu, %lu, %lu\n", fragment_id, fragment_offset,
969 message_id, group_generation);
971 return GNUNET_SYSERR;
974 if (GNUNET_OK != channel_key_store (plugin, channel_key))
975 return GNUNET_SYSERR;
977 struct GNUNET_MY_QueryParam params_insert[] = {
978 GNUNET_MY_query_param_auto_from_type (channel_key),
979 GNUNET_MY_query_param_uint64 (&hop_counter),
980 GNUNET_MY_query_param_auto_from_type (&msg->signature),
981 GNUNET_MY_query_param_auto_from_type (&msg->purpose),
982 GNUNET_MY_query_param_uint64 (&fragment_id),
983 GNUNET_MY_query_param_uint64 (&fragment_offset),
984 GNUNET_MY_query_param_uint64 (&message_id),
985 GNUNET_MY_query_param_uint64 (&group_generation),
986 GNUNET_MY_query_param_uint64 (&flags),
987 GNUNET_MY_query_param_uint32 (&psycstore_flags),
988 GNUNET_MY_query_param_fixed_size (&msg[1], ntohs (msg->header.size)
990 GNUNET_MY_query_param_end
993 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
997 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
998 "mysql execute prepared", stmt);
999 return GNUNET_SYSERR;
1002 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1004 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1005 "mysql_stmt_reset", stmt);
1006 return GNUNET_SYSERR;
1013 * Set additional flags for a given message.
1015 * They are OR'd with any existing flags set.
1017 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1020 message_add_flags (void *cls,
1021 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1022 uint64_t message_id,
1023 uint64_t psycstore_flags)
1025 struct Plugin *plugin = cls;
1027 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->update_message_flags;
1030 int ret = GNUNET_SYSERR;
1032 struct GNUNET_MY_QueryParam params_update[] = {
1033 GNUNET_MY_query_param_uint64 (&psycstore_flags),
1034 GNUNET_MY_query_param_auto_from_type (channel_key),
1035 GNUNET_MY_query_param_uint64 (&message_id),
1036 GNUNET_MY_query_param_end
1039 sql_ret = GNUNET_MY_exec_prepared (plugin->mc,
1048 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1049 "mysql execute prepared", stmt);
1052 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1054 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1055 "mysql_stmt_reset", stmt);
1056 return GNUNET_SYSERR;
1064 fragment_row (struct GNUNET_MYSQL_StatementHandle *stmt,
1065 GNUNET_PSYCSTORE_FragmentCallback cb,
1069 uint32_t hop_counter;
1070 void *signature = NULL;
1071 void *purpose = NULL;
1072 size_t signature_size;
1073 size_t purpose_size;
1075 uint64_t fragment_id;
1076 uint64_t fragment_offset;
1077 uint64_t message_id;
1078 uint64_t group_generation;
1082 int ret = GNUNET_SYSERR;
1084 struct GNUNET_MULTICAST_MessageHeader *mp;
1089 struct GNUNET_MY_ResultSpec results[] = {
1090 GNUNET_MY_result_spec_uint32 (&hop_counter),
1091 GNUNET_MY_result_spec_variable_size (&signature, &signature_size),
1092 GNUNET_MY_result_spec_variable_size (&purpose, &purpose_size),
1093 GNUNET_MY_result_spec_uint64 (&fragment_id),
1094 GNUNET_MY_result_spec_uint64 (&fragment_offset),
1095 GNUNET_MY_result_spec_uint64 (&message_id),
1096 GNUNET_MY_result_spec_uint64 (&group_generation),
1097 GNUNET_MY_result_spec_uint64 (&msg_flags),
1098 GNUNET_MY_result_spec_uint64 (&flags),
1099 GNUNET_MY_result_spec_variable_size (&buf,
1101 GNUNET_MY_result_spec_end
1104 sql_ret = GNUNET_MY_extract_result (stmt,
1109 if (ret != GNUNET_OK)
1114 mp = GNUNET_malloc (sizeof (*mp) + buf_size);
1116 mp->header.size = htons (sizeof (*mp) + buf_size);
1117 mp->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
1118 mp->hop_counter = htonl (hop_counter);
1119 GNUNET_memcpy (&mp->signature,
1122 GNUNET_memcpy (&mp->purpose,
1125 mp->fragment_id = GNUNET_htonll (fragment_id);
1126 mp->fragment_offset = GNUNET_htonll (fragment_offset);
1127 mp->message_id = GNUNET_htonll (message_id);
1128 mp->group_generation = GNUNET_htonll (group_generation);
1129 mp->flags = htonl(msg_flags);
1131 GNUNET_memcpy (&mp[1],
1136 (enum GNUNET_PSYCSTORE_MessageFlags) flags);
1138 GNUNET_MY_cleanup_result (results);
1142 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1143 "mysql extract_result", stmt);
1151 fragment_select (struct Plugin *plugin, struct GNUNET_MYSQL_StatementHandle *stmt,
1152 struct GNUNET_MY_QueryParam *params,
1153 uint64_t *returned_fragments,
1154 GNUNET_PSYCSTORE_FragmentCallback cb, void *cb_cls)
1156 int ret = GNUNET_SYSERR;
1159 sql_ret = GNUNET_MY_exec_prepared (plugin->mc,
1165 if (ret != GNUNET_OK)
1169 ret = fragment_row (stmt, cb, cb_cls);
1170 (*returned_fragments)++;
1173 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1174 "mysql exec_prepared", stmt);
1181 * Retrieve a message fragment range by fragment ID.
1183 * @see GNUNET_PSYCSTORE_fragment_get()
1185 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1188 fragment_get (void *cls,
1189 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1190 uint64_t first_fragment_id,
1191 uint64_t last_fragment_id,
1192 uint64_t *returned_fragments,
1193 GNUNET_PSYCSTORE_FragmentCallback cb,
1196 struct Plugin *plugin = cls;
1197 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_fragments;
1198 int ret = GNUNET_SYSERR;
1199 *returned_fragments = 0;
1201 struct GNUNET_MY_QueryParam params_select[] = {
1202 GNUNET_MY_query_param_auto_from_type (channel_key),
1203 GNUNET_MY_query_param_uint64 (&first_fragment_id),
1204 GNUNET_MY_query_param_uint64 (&last_fragment_id),
1205 GNUNET_MY_query_param_end
1208 ret = fragment_select (plugin, stmt, params_select, returned_fragments, cb, cb_cls);
1210 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1212 LOG_MYSQL (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1213 "mysql_stmt_reset", stmt);
1214 return GNUNET_SYSERR;
1222 * Retrieve a message fragment range by fragment ID.
1224 * @see GNUNET_PSYCSTORE_fragment_get_latest()
1226 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1229 fragment_get_latest (void *cls,
1230 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1231 uint64_t fragment_limit,
1232 uint64_t *returned_fragments,
1233 GNUNET_PSYCSTORE_FragmentCallback cb,
1236 struct Plugin *plugin = cls;
1238 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_latest_fragments;
1240 int ret = GNUNET_SYSERR;
1241 *returned_fragments = 0;
1243 struct GNUNET_MY_QueryParam params_select[] = {
1244 GNUNET_MY_query_param_auto_from_type (channel_key),
1245 GNUNET_MY_query_param_uint64 (&fragment_limit),
1246 GNUNET_MY_query_param_end
1249 ret = fragment_select (plugin, stmt, params_select, returned_fragments, cb, cb_cls);
1251 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1253 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1254 "mysql_stmt_reset", stmt);
1255 return GNUNET_SYSERR;
1263 * Retrieve all fragments of a message ID range.
1265 * @see GNUNET_PSYCSTORE_message_get()
1267 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1270 message_get (void *cls,
1271 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1272 uint64_t first_message_id,
1273 uint64_t last_message_id,
1274 uint64_t fragment_limit,
1275 uint64_t *returned_fragments,
1276 GNUNET_PSYCSTORE_FragmentCallback cb,
1279 struct Plugin *plugin = cls;
1281 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_messages;
1283 int ret = GNUNET_SYSERR;
1284 *returned_fragments = 0;
1286 struct GNUNET_MY_QueryParam params_select[] = {
1287 GNUNET_MY_query_param_auto_from_type (channel_key),
1288 GNUNET_MY_query_param_uint64 (&first_message_id),
1289 GNUNET_MY_query_param_uint64 (&last_message_id),
1290 GNUNET_MY_query_param_uint64 (&fragment_limit),
1291 GNUNET_MY_query_param_end
1294 ret = fragment_select (plugin, stmt, params_select, returned_fragments, cb, cb_cls);
1296 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1298 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1299 "mysql_stmt_reset", stmt);
1300 return GNUNET_SYSERR;
1308 * Retrieve all fragments of the latest messages.
1310 * @see GNUNET_PSYCSTORE_message_get_latest()
1312 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1315 message_get_latest (void *cls,
1316 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1317 uint64_t message_limit,
1318 uint64_t *returned_fragments,
1319 GNUNET_PSYCSTORE_FragmentCallback cb,
1322 struct Plugin *plugin = cls;
1324 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_latest_messages;
1326 int ret = GNUNET_SYSERR;
1327 *returned_fragments = 0;
1329 struct GNUNET_MY_QueryParam params_select[] = {
1330 GNUNET_MY_query_param_auto_from_type (channel_key),
1331 GNUNET_MY_query_param_auto_from_type (channel_key),
1332 GNUNET_MY_query_param_uint64 (&message_limit),
1333 GNUNET_MY_query_param_end
1336 ret = fragment_select (plugin, stmt, params_select, returned_fragments, cb, cb_cls);
1338 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1340 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1341 "mysql_stmt_reset", stmt);
1342 return GNUNET_SYSERR;
1350 * Retrieve a fragment of message specified by its message ID and fragment
1353 * @see GNUNET_PSYCSTORE_message_get_fragment()
1355 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1358 message_get_fragment (void *cls,
1359 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1360 uint64_t message_id,
1361 uint64_t fragment_offset,
1362 GNUNET_PSYCSTORE_FragmentCallback cb,
1365 struct Plugin *plugin = cls;
1367 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_message_fragment;
1369 int ret = GNUNET_SYSERR;
1371 struct GNUNET_MY_QueryParam params_select[] = {
1372 GNUNET_MY_query_param_auto_from_type (channel_key),
1373 GNUNET_MY_query_param_uint64 (&message_id),
1374 GNUNET_MY_query_param_uint64 (&fragment_offset),
1375 GNUNET_MY_query_param_end
1378 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1382 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1383 "mysql execute prepared", stmt);
1384 return GNUNET_SYSERR;
1387 ret = fragment_row (stmt, cb, cb_cls);
1389 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1391 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1392 "mysql_stmt_reset", stmt);
1393 return GNUNET_SYSERR;
1400 * Retrieve the max. values of message counters for a channel.
1402 * @see GNUNET_PSYCSTORE_counters_get()
1404 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1407 counters_message_get (void *cls,
1408 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1409 uint64_t *max_fragment_id,
1410 uint64_t *max_message_id,
1411 uint64_t *max_group_generation)
1413 struct Plugin *plugin = cls;
1415 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_counters_message;
1417 int ret = GNUNET_SYSERR;
1419 struct GNUNET_MY_QueryParam params_select[] = {
1420 GNUNET_MY_query_param_auto_from_type (channel_key),
1421 GNUNET_MY_query_param_end
1424 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1428 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1429 "mysql execute prepared", stmt);
1430 return GNUNET_SYSERR;
1433 struct GNUNET_MY_ResultSpec results_select[] = {
1434 GNUNET_MY_result_spec_uint64 (max_fragment_id),
1435 GNUNET_MY_result_spec_uint64 (max_message_id),
1436 GNUNET_MY_result_spec_uint64 (max_group_generation),
1437 GNUNET_MY_result_spec_end
1440 ret = GNUNET_MY_extract_result (stmt,
1443 if (GNUNET_OK != ret)
1445 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1446 "mysql extract_result", stmt);
1447 return GNUNET_SYSERR;
1450 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1452 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1453 "mysql_stmt_reset", stmt);
1454 return GNUNET_SYSERR;
1461 * Retrieve the max. values of state counters for a channel.
1463 * @see GNUNET_PSYCSTORE_counters_get()
1465 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1468 counters_state_get (void *cls,
1469 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1470 uint64_t *max_state_message_id)
1472 struct Plugin *plugin = cls;
1474 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_counters_state;
1476 int ret = GNUNET_SYSERR;
1478 struct GNUNET_MY_QueryParam params_select[] = {
1479 GNUNET_MY_query_param_auto_from_type (channel_key),
1480 GNUNET_MY_query_param_end
1483 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1487 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1488 "mysql execute prepared", stmt);
1489 return GNUNET_SYSERR;
1492 struct GNUNET_MY_ResultSpec results_select[] = {
1493 GNUNET_MY_result_spec_uint64 (max_state_message_id),
1494 GNUNET_MY_result_spec_end
1497 ret = GNUNET_MY_extract_result (stmt,
1500 if (GNUNET_OK != ret)
1502 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1503 "mysql extract_result", stmt);
1504 return GNUNET_SYSERR;
1507 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1509 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1510 "mysql_stmt_reset", stmt);
1511 return GNUNET_SYSERR;
1519 * Assign a value to a state variable.
1521 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1524 state_assign (struct Plugin *plugin, struct GNUNET_MYSQL_StatementHandle *stmt,
1525 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1526 const char *name, const void *value, size_t value_size)
1528 int ret = GNUNET_SYSERR;
1530 struct GNUNET_MY_QueryParam params[] = {
1531 GNUNET_MY_query_param_auto_from_type (channel_key),
1532 GNUNET_MY_query_param_string (name),
1533 GNUNET_MY_query_param_auto_from_type (value),
1534 GNUNET_MY_query_param_end
1537 ret = GNUNET_MY_exec_prepared (plugin->mc,
1541 if (GNUNET_OK != ret)
1543 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1544 "mysql execute prepared", stmt);
1545 return GNUNET_SYSERR;
1548 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1550 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1551 "mysql_stmt_reset", stmt);
1552 return GNUNET_SYSERR;
1560 update_message_id (struct Plugin *plugin, struct GNUNET_MYSQL_StatementHandle *stmt,
1561 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1562 uint64_t message_id)
1564 struct GNUNET_MY_QueryParam params[] = {
1565 GNUNET_MY_query_param_uint64 (&message_id),
1566 GNUNET_MY_query_param_auto_from_type (channel_key),
1567 GNUNET_MY_query_param_end
1570 if (GNUNET_OK != GNUNET_MY_exec_prepared (plugin->mc,
1574 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1575 "mysql execute prepared", stmt);
1576 return GNUNET_SYSERR;
1579 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1581 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1582 "mysql_stmt_reset", stmt);
1583 return GNUNET_SYSERR;
1591 * Begin modifying current state.
1594 state_modify_begin (void *cls,
1595 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1596 uint64_t message_id, uint64_t state_delta)
1598 struct Plugin *plugin = cls;
1600 if (state_delta > 0)
1603 * We can only apply state modifiers in the current message if modifiers in
1604 * the previous stateful message (message_id - state_delta) were already
1608 uint64_t max_state_message_id = 0;
1609 int ret = counters_state_get (plugin, channel_key, &max_state_message_id);
1613 case GNUNET_NO: // no state yet
1620 if (max_state_message_id < message_id - state_delta)
1621 return GNUNET_NO; /* some stateful messages not yet applied */
1622 else if (message_id - state_delta < max_state_message_id)
1623 return GNUNET_NO; /* changes already applied */
1626 if (TRANSACTION_NONE != plugin->transaction)
1628 /** @todo FIXME: wait for other transaction to finish */
1629 return GNUNET_SYSERR;
1631 return transaction_begin (plugin, TRANSACTION_STATE_MODIFY);
1636 * Set the current value of state variable.
1638 * @see GNUNET_PSYCSTORE_state_modify()
1640 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1643 state_modify_op (void *cls,
1644 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1645 enum GNUNET_PSYC_Operator op,
1646 const char *name, const void *value, size_t value_size)
1648 struct Plugin *plugin = cls;
1649 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1653 case GNUNET_PSYC_OP_ASSIGN:
1654 return state_assign (plugin, plugin->insert_state_current, channel_key,
1655 name, value, value_size);
1657 default: /** @todo implement more state operations */
1659 return GNUNET_SYSERR;
1665 * End modifying current state.
1668 state_modify_end (void *cls,
1669 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1670 uint64_t message_id)
1672 struct Plugin *plugin = cls;
1673 GNUNET_assert (TRANSACTION_STATE_MODIFY == plugin->transaction);
1676 GNUNET_OK == exec_channel (plugin, plugin->delete_state_empty, channel_key)
1677 && GNUNET_OK == update_message_id (plugin,
1678 plugin->update_max_state_message_id,
1679 channel_key, message_id)
1680 && GNUNET_OK == transaction_commit (plugin)
1681 ? GNUNET_OK : GNUNET_SYSERR;
1686 * Begin state synchronization.
1689 state_sync_begin (void *cls,
1690 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1692 struct Plugin *plugin = cls;
1693 return exec_channel (plugin, plugin->delete_state_sync, channel_key);
1698 * Assign current value of a state variable.
1700 * @see GNUNET_PSYCSTORE_state_modify()
1702 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1705 state_sync_assign (void *cls,
1706 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1707 const char *name, const void *value, size_t value_size)
1709 struct Plugin *plugin = cls;
1710 return state_assign (cls, plugin->insert_state_sync, channel_key,
1711 name, value, value_size);
1716 * End modifying current state.
1719 state_sync_end (void *cls,
1720 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1721 uint64_t max_state_message_id,
1722 uint64_t state_hash_message_id)
1724 struct Plugin *plugin = cls;
1725 int ret = GNUNET_SYSERR;
1727 if (TRANSACTION_NONE != plugin->transaction)
1729 /** @todo FIXME: wait for other transaction to finish */
1730 return GNUNET_SYSERR;
1733 GNUNET_OK == transaction_begin (plugin, TRANSACTION_STATE_SYNC)
1734 && GNUNET_OK == exec_channel (plugin, plugin->delete_state, channel_key)
1735 && GNUNET_OK == exec_channel (plugin, plugin->insert_state_from_sync,
1737 && GNUNET_OK == exec_channel (plugin, plugin->delete_state_sync,
1739 && GNUNET_OK == update_message_id (plugin,
1740 plugin->update_state_hash_message_id,
1741 channel_key, state_hash_message_id)
1742 && GNUNET_OK == update_message_id (plugin,
1743 plugin->update_max_state_message_id,
1744 channel_key, max_state_message_id)
1745 && GNUNET_OK == transaction_commit (plugin)
1747 : transaction_rollback (plugin);
1753 * Delete the whole state.
1755 * @see GNUNET_PSYCSTORE_state_reset()
1757 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1760 state_reset (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1762 struct Plugin *plugin = cls;
1763 return exec_channel (plugin, plugin->delete_state, channel_key);
1768 * Update signed values of state variables in the state store.
1770 * @see GNUNET_PSYCSTORE_state_hash_update()
1772 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1775 state_update_signed (void *cls,
1776 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key)
1778 struct Plugin *plugin = cls;
1779 return exec_channel (plugin, plugin->update_state_signed, channel_key);
1784 * Retrieve a state variable by name.
1786 * @see GNUNET_PSYCSTORE_state_get()
1788 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1791 state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1792 const char *name, GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
1794 struct Plugin *plugin = cls;
1795 int ret = GNUNET_SYSERR;
1798 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_state_one;
1800 struct GNUNET_MY_QueryParam params_select[] = {
1801 GNUNET_MY_query_param_auto_from_type (channel_key),
1802 GNUNET_MY_query_param_string (name),
1803 GNUNET_MY_query_param_end
1806 void *value_current = NULL;
1807 size_t value_size = 0;
1809 struct GNUNET_MY_ResultSpec results[] = {
1810 GNUNET_MY_result_spec_variable_size (&value_current, &value_size),
1811 GNUNET_MY_result_spec_end
1814 GNUNET_MY_exec_prepared (plugin->mc,
1819 sql_ret = GNUNET_MY_extract_result (stmt,
1828 ret = cb (cb_cls, name, value_current,
1832 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1833 "mysql extract_result", stmt);
1836 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1838 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1839 "mysql_stmt_reset", stmt);
1840 return GNUNET_SYSERR;
1848 * Retrieve all state variables for a channel with the given prefix.
1850 * @see GNUNET_PSYCSTORE_state_get_prefix()
1852 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1855 state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1856 const char *name, GNUNET_PSYCSTORE_StateCallback cb,
1859 struct Plugin *plugin = cls;
1860 int ret = GNUNET_SYSERR;
1862 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_state_prefix;
1864 uint32_t name_len = (uint32_t) strlen (name);
1866 struct GNUNET_MY_QueryParam params_select[] = {
1867 GNUNET_MY_query_param_auto_from_type (channel_key),
1868 GNUNET_MY_query_param_string (name),
1869 GNUNET_MY_query_param_uint32 (&name_len),
1870 GNUNET_MY_query_param_string (name),
1871 GNUNET_MY_query_param_end
1875 void *value_current = NULL;
1876 size_t value_size = 0;
1878 struct GNUNET_MY_ResultSpec results[] = {
1879 GNUNET_MY_result_spec_string (&name2),
1880 GNUNET_MY_result_spec_variable_size (&value_current, &value_size),
1881 GNUNET_MY_result_spec_end
1888 GNUNET_MY_exec_prepared (plugin->mc,
1891 sql_ret = GNUNET_MY_extract_result (stmt,
1896 if (ret != GNUNET_OK)
1900 ret = cb (cb_cls, (const char *) name2,
1904 if (ret != GNUNET_YES)
1905 sql_ret = GNUNET_NO;
1908 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1909 "mysql extract_result", stmt);
1912 while (sql_ret == GNUNET_YES);
1914 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1916 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1917 "mysql_stmt_reset", stmt);
1918 return GNUNET_SYSERR;
1926 * Retrieve all signed state variables for a channel.
1928 * @see GNUNET_PSYCSTORE_state_get_signed()
1930 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
1933 state_get_signed (void *cls,
1934 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
1935 GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
1937 struct Plugin *plugin = cls;
1938 int ret = GNUNET_SYSERR;
1940 struct GNUNET_MYSQL_StatementHandle *stmt = plugin->select_state_signed;
1942 struct GNUNET_MY_QueryParam params_select[] = {
1943 GNUNET_MY_query_param_auto_from_type (channel_key),
1944 GNUNET_MY_query_param_end
1950 void *value_signed = NULL;
1951 size_t value_size = 0;
1953 struct GNUNET_MY_ResultSpec results[] = {
1954 GNUNET_MY_result_spec_string (&name),
1955 GNUNET_MY_result_spec_variable_size (&value_signed, &value_size),
1956 GNUNET_MY_result_spec_end
1961 GNUNET_MY_exec_prepared (plugin->mc,
1964 sql_ret = GNUNET_MY_extract_result (stmt,
1970 if (ret != GNUNET_OK)
1974 ret = cb (cb_cls, (const char *) name,
1978 if (ret != GNUNET_YES)
1979 sql_ret = GNUNET_NO;
1982 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1983 "mysql extract_result", stmt);
1986 while (sql_ret == GNUNET_YES);
1988 if (0 != mysql_stmt_reset (GNUNET_MYSQL_statement_get_stmt (stmt)))
1990 LOG_MYSQL(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1991 "mysql_stmt_reset", stmt);
1992 return GNUNET_SYSERR;
2000 * Entry point for the plugin.
2002 * @param cls The struct GNUNET_CONFIGURATION_Handle.
2003 * @return NULL on error, otherwise the plugin context
2006 libgnunet_plugin_psycstore_mysql_init (void *cls)
2008 static struct Plugin plugin;
2009 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
2010 struct GNUNET_PSYCSTORE_PluginFunctions *api;
2012 if (NULL != plugin.cfg)
2013 return NULL; /* can only initialize once! */
2014 memset (&plugin, 0, sizeof (struct Plugin));
2016 if (GNUNET_OK != database_setup (&plugin))
2018 database_shutdown (&plugin);
2021 api = GNUNET_new (struct GNUNET_PSYCSTORE_PluginFunctions);
2023 api->membership_store = &mysql_membership_store;
2024 api->membership_test = &membership_test;
2025 api->fragment_store = &fragment_store;
2026 api->message_add_flags = &message_add_flags;
2027 api->fragment_get = &fragment_get;
2028 api->fragment_get_latest = &fragment_get_latest;
2029 api->message_get = &message_get;
2030 api->message_get_latest = &message_get_latest;
2031 api->message_get_fragment = &message_get_fragment;
2032 api->counters_message_get = &counters_message_get;
2033 api->counters_state_get = &counters_state_get;
2034 api->state_modify_begin = &state_modify_begin;
2035 api->state_modify_op = &state_modify_op;
2036 api->state_modify_end = &state_modify_end;
2037 api->state_sync_begin = &state_sync_begin;
2038 api->state_sync_assign = &state_sync_assign;
2039 api->state_sync_end = &state_sync_end;
2040 api->state_reset = &state_reset;
2041 api->state_update_signed = &state_update_signed;
2042 api->state_get = &state_get;
2043 api->state_get_prefix = &state_get_prefix;
2044 api->state_get_signed = &state_get_signed;
2046 LOG (GNUNET_ERROR_TYPE_INFO, _("Mysql database running\n"));
2052 * Exit point from the plugin.
2054 * @param cls The plugin context (as returned by "init")
2055 * @return Always NULL
2058 libgnunet_plugin_psycstore_mysql_done (void *cls)
2060 struct GNUNET_PSYCSTORE_PluginFunctions *api = cls;
2061 struct Plugin *plugin = api->cls;
2063 database_shutdown (plugin);
2066 LOG (GNUNET_ERROR_TYPE_DEBUG, "Mysql plugin is finished\n");
2070 /* end of plugin_psycstore_mysql.c */